gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:18:47 25 4
gpt4 key购买 nike

我需要帮助将字符串数组列表“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列表来计算平均值和标准差。祝你好运!

关于java - 如何在数组列表中存储字符串数组列表的哈希码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31495982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com