I have been looking into the speeds of different Java collection types and have come across something weird. I am adding 1,000,000 objects from a static array to a different collection type and returning the time required. This part of the code works fine.
Under further investigation I noticed that the TreeSet
is not receiving all of the 1,000,000 objects, and is receiving a different amount each time. Below is the method to transfer the objects from an array to the TreeSet
:
public int treeSet(int num)
{
Date before = new Date();
for(int i=0; i<num; i++)
{
treeSet.add(personsArray[i]);
}
Date after = new Date();
return (int) (after.getTime() - before.getTime());
}
Below is the code which calls the treeSet() method and tests for its size.
System.out.println("\tTree set with 1,000,000 objects--" + t.treeSet(1000000));
System.out.println("Tree set contains " + t.treeSet.size() + " elements");
The output of this is:
Tree set with 1,000,000 objects--1192
Tree set contains 975741 elements
I'm hoping someone can explain to me why the TreeSet
is not receiving all of the objects and why it is receiving inconsistent amounts.