如何在数组列表中存储字符串数组列表的哈希码

时间:2015-07-18 21:57:15

标签: java arrays hash linked-list

我需要帮助存储字符串数组列表的哈希码" randomNumbersStrg"在另一个数组列表中。我的导师希望我们生成100万个随机整数,将它们转换为字符串,然后获取每个字符串的哈希码。有人可以帮我生成哈希码并将它们存储到链表中吗?这是我到目前为止所做的:

import java.util.*;

public class dataStuctures
{

  public static void main(String[] args)
  {  
     int MAXIMUM = 5;//initializing the maximum integer
     int MINIMUM = 1;//initializing the minimum integer

     Random randomGenerator = new Random();//initializing the generation of random integers

     int range = MAXIMUM - MINIMUM + 1;//setting the range of integers from 1 to 1,000,000

     ArrayList<Integer> randomNumbers = new ArrayList<Integer>(5);//initializing an ArrayList to store the generated random integers with the capacity of 1,000,000

     //ArrayList<String> randomNumbersStrg = new ArrayList<String>();//initializing an ArrayList to store store the generated hashcodes into a string

     for (int index = 1; index <= 5; ++index)//for loop to generate 1,000,000 random integers in a range from 1 to 1,000,000
     {
           int randomInt = randomGenerator.nextInt(range) + MINIMUM;

           randomNumbers.add(randomInt);//storing randomly generated numbers in a vector


     }//end of for loop for random number generation and storage in an ArrayList

     System.out.println("random numbers= " + randomNumbers);
     System.out.println("ArrayList size: " + randomNumbers.size());



     ArrayList<String> randomNumbersStrg = new ArrayList<String>(randomNumbers.size());
     ArrayList<String> randomNumbersHashCodes = new ArrayList<String>(randomNumbers.size());
     for (Integer myInt : randomNumbers)
     {
        randomNumbersStrg.add(String.valueOf(myInt));
        randomNumbersHashCodes.add(randomNumbersStrg.get(myInt));
     }




     // to test to make sure the integers converted
     String first = randomNumbersStrg.get(0);
     System.out.println("hash codeest = " + first.hashCode());
     String second = randomNumbersStrg.get(1);
     String third = randomNumbersStrg.get(2);
     String fourth = randomNumbersStrg.get(3);
     String fifth = randomNumbersStrg.get(4);
     System.out.println("\nfirst = " + first);
     System.out.println("second = " + second);
     System.out.println("third = " + third);
     System.out.println("fourth = " + fourth);
     System.out.println("fifth = " + fifth);


     List<String> linkedList = new LinkedList<String>();//initializing Linked List
     linkedList.addAll(randomNumbersStrg);//adding generated hashcodes to Linked List







  }//end of main method

  private static long[] randomNumbers(int index2)
  {
     // TODO Auto-generated method stub
     return null;
  }
}

1 个答案:

答案 0 :(得分:1)

只需使用一个LinkedList来存储哈希码,使用ArrayList来存储搜索时间:

List<Integer> hashCodes = new LinkedList<>();
for (int index = 0; index < 1_000_000; ++index) {
    int randomInt = randomGenerator.nextInt(range) + MINIMUM;
    hashCodes.add(String.valueOf(randomInt).hashCode());
}

这将用于哈希码。

然后遍历hashCodes列表以搜索每个元素,计算所需的时间:

List<Long> durations = new ArrayList<>(hashCodes.size());
for (int n : hashCodes) {
    long start = System.nanoTime();
    hashCodes.contains(n); // true
    long end = System.nanoTime();
    durations.add(end - start);
}

这应该这样做。然后你有durations列表来计算平均值和标准值。祝你好运!