簡體   English   中英

如何在數組列表中存儲字符串數組列表的哈希碼

[英]How to store hash codes of a String Array list in an array 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;
  }
}

只需使用一個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列表來計算平均值和標准差。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM