3

Is the contains method on TreeSet (Since it is already sorted per default) faster than say HashSet?

The reason I ask is that Collections.binarySearch is quite fast if the List is sorted, so I am thinking that maybe the contains method for TreeSet might be the same.

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143

1 Answers1

5

From the javadoc of TreeSet:

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

From the javadoc of HashSet:

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.

So the answer is no.

Looking at the implementation (JDK 1.7 oracle), treeset.contains (resp. hashtree) relies on treemap.containsKey (resp. hashmap) method. containsKey loops over one hash bucket in hashmap (which possibly contains only one item), whereas it loops over the whole map, moving from node to node in treemap, using the compareTo method. If your item is the largest or the smallest, this can take significantly more time.

Finally, I just ran a quick test (yes I know, not very reliable) with a tree containing 1m integers and looking for one of the 2 largest, which forces the treeset to browse the whole set. HashSet is quicker by a factor of 50.

assylias
  • 321,522
  • 82
  • 660
  • 783