0

I need help storing the hash codes of the string array list "randomNumbersStrg" in another array list. my instructor wants us to generate 1 million random integers, convert them into strings and then get the hash codes for each string. Could someone help me generate the hash codes and store them into a linked list? here is what i have so far:

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;
  }
}
5
  • 1
    In java every object has a hashCode() method that returns an int. Just create another ArrayList<Integer> and store the result of each string's hashCode(). Commented Jul 18, 2015 at 22:42
  • Can you create another array list and use the numbers you stored in the original array list to get the hash codes? Can I call the strings from array list randomNumbersStrg? Commented Jul 18, 2015 at 22:46
  • 1
    Do you need everything? I mean if you need the original numbers and the String version of these numbers besides the hash codes, or if you're ok just with the hash codes. Commented Jul 18, 2015 at 22:56
  • I need to put the hash codes into a linked list and then For each of the million hash codes, capture the time it takes to determine if the Linked List contains the hash code. Use System.nanoTime() to get the time in nano seconds. Commented Jul 18, 2015 at 23:21
  • 1
    I also need to Determine the mean and standard deviation of the timing performance of the Linked List for searching. but i will try to do that myself after you help with the linked list. Commented Jul 19, 2015 at 2:11

1 Answer 1

1

Just use one LinkedList to store the hash codes and an ArrayList to store the search times:

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());
}

This will do it for the hash codes.

Then iterate over the hashCodes list to search for every element, calculating how long it takes:

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);
}

This should do it. Then you have durations list to calculate average and std dev. Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

This works perfectly thank you Federico. I now have to input the million hash codes into a tree. Any help or links I could go to find that out? Heres exactly what I have to do: also insert the million hash codes into a tree using the. For each of the million hash codes, capture the time it takes to determine if the Linked List contains the hash code. determine the mean and standard deviation of the timing performance of the Tree for searching.Then, using the Java Container Class for LinkedList determine the mean and standard deviation of the timing performance.
@ChristopherSchubert With a tree you have to do the same, but using a tree i.e. TreeSet instead of a LinkedList. Then you calculate search duration exactly the same way. The only difference is that tree implementations in Java do not allow duplicates, so size of the tree would be less than the size of the list.
Thank you for all your help Federico.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.