-1

How to add 20.000.000 random integers to HashSet in Java? It's taking very long time. This is my code:

final int SET_SIZE=20000000;
Set<Integer> set1 = new HashSet<Integer>();
for(int i=1;i<=SET_SIZE;i++)
    set1.add(i);
Frou-Frou Fox
  • 194
  • 1
  • 13
One Any
  • 115
  • 1
  • 1
  • 4

2 Answers2

0

As you add entries (random or otherwise) to your HashSet, the Set has to grow in size. Each time, when it grows beyond a limit, it has to reprocess all the entries to ensure they end up in the proper bin for the larger sized set.

You can speed things up by telling the HashSet how big you expect it to be ahead of time. In this case, you know exactly how big it is, so you can preallocate the correctly sized structure.

Set<Integer> set1 = new HashSet<>(SET_SIZE);

JSYK:

HashSet<Integer> may not be the best data structure for your use case. If you are only storing non-negative Integer values, from 0 up to some limit (SET_SIZE), and your set is not sparely populated, you might want to consider using a BitSet instead.

A BitSet stores the presence or absence of each value as a single bit. Therefore, the memory requirement of BitSet is a function of the largest integer ever stored in the set; a BitSet that can store all the Integer values from 0 to 20 million only takes 2.5 million bytes of storage.

In contrast, the memory requirement of the HashSet<Integer> is a function of the number entries stored in it. If each entry takes 32 bytes, then after the 78125 entries have been stored in the HashSet<Integer>, it would take more memory than a BitSet of 20 million bits.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
0
final int SET_SIZE=20000000; 
Set<Integer> set1 = new HashSet<Integer>();
Random rand = new Random();
for(int i=1;i<=SET_SIZE;i++)
  et1.add(rand.nextInt());
Arjun Kay
  • 308
  • 1
  • 12