Hashset vs Treeset

heymatthew

I've always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet. From a CS background, I don't think it matters all that much which you use, and I don't care to mess around with hash functions and buckets (in the case of Java).

In which cases should I use a HashSet over a TreeSet?

sactiw

HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.

HashSet

  • the class offers constant time performance for the basic operations (add, remove, contains and size).
  • it does not guarantee that the order of elements will remain constant over time
  • iteration performance depends on the initial capacity and the load factor of the HashSet.
    • It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.

TreeSet

  • guarantees log(n) time cost for the basic operations (add, remove and contains)
  • guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via its constructor) (implements SortedSet)
  • doesn't offer any tuning parameters for iteration performance
  • offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc

Important points:

  • Both guarantee duplicate-free collection of elements
  • It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal.
  • None of these implementations are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
  • LinkedHashSet is in some sense intermediate between HashSet and TreeSet. Implemented as a hash table with a linked list running through it, however,it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet.

So a choice of usage depends entirely on your needs but I feel that even if you need an ordered collection then you should still prefer HashSet to create the Set and then convert it into TreeSet.

  • e.g. SortedSet<String> s = new TreeSet<String>(hashSet);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HashSet vs TreeSet vs LinkedHashSet on basis of adding duplicate value

From Dev

Java: Hashset Vs TreeSet - when should I use over the other?

From Java

Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

From Dev

HashSet and TreeSet performance test

From Dev

Java hashset and treeset

From Dev

HashSet and TreeSet in Java

From Dev

HashSet and TreeSet performance test

From Dev

TreeSet vs HashSet speed for small set size, when O(1) vs O(log n) doesn't matter

From Dev

SharedPreferences - CastException: HashSet cannot be cast to TreeSet

From Dev

can't convert hashset to treeset with object collection

From Dev

How to create TreeSet using an existing HashSet with data in it?

From Dev

removeAll() in ArrayList vs HashSet

From Dev

Should I use a `HashSet` or a `TreeSet` for a very large dataset?

From Dev

HashSet vs ArrayList contains performance

From Dev

"Contains" implementation of ArrayList vs HashSet

From Dev

Java HashSet vs Array Performance

From Dev

Empty HashSet - Count vs Any

From Dev

ArrayList vs HashSet as the simplest Collection

From Dev

TreeSet vs Java 8 Streams performance

From Dev

LinkedHashMap vs HashMap, LinkedHashSet vs HashSet

From Dev

What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

From Dev

Why adding null in HashSet does not throw Exception,but adding null in TreeSet throw Exception

From Dev

Scala vs Java performance (HashSet and bigram generation)

From Dev

HashSet vs ArrayList CPU usage is high

From Dev

ArrayList vs HashSet, which is better for memory performance?

From Dev

What is increased cost of TreeSet vs LinkedHashSet and TreeMap over LinkedHashMap?

From Dev

List<T> vs HashSet<T> - dynamic collection choice is efficient or not?

From Dev

Which approach is faster when when searching for an element? HashSet vs .indexOf()

From Dev

C# Time complexity of Array[T].Contains(T item) vs HashSet<T>.Contains(T item)

Related Related

  1. 1

    HashSet vs TreeSet vs LinkedHashSet on basis of adding duplicate value

  2. 2

    Java: Hashset Vs TreeSet - when should I use over the other?

  3. 3

    Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

  4. 4

    HashSet and TreeSet performance test

  5. 5

    Java hashset and treeset

  6. 6

    HashSet and TreeSet in Java

  7. 7

    HashSet and TreeSet performance test

  8. 8

    TreeSet vs HashSet speed for small set size, when O(1) vs O(log n) doesn't matter

  9. 9

    SharedPreferences - CastException: HashSet cannot be cast to TreeSet

  10. 10

    can't convert hashset to treeset with object collection

  11. 11

    How to create TreeSet using an existing HashSet with data in it?

  12. 12

    removeAll() in ArrayList vs HashSet

  13. 13

    Should I use a `HashSet` or a `TreeSet` for a very large dataset?

  14. 14

    HashSet vs ArrayList contains performance

  15. 15

    "Contains" implementation of ArrayList vs HashSet

  16. 16

    Java HashSet vs Array Performance

  17. 17

    Empty HashSet - Count vs Any

  18. 18

    ArrayList vs HashSet as the simplest Collection

  19. 19

    TreeSet vs Java 8 Streams performance

  20. 20

    LinkedHashMap vs HashMap, LinkedHashSet vs HashSet

  21. 21

    What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

  22. 22

    Why adding null in HashSet does not throw Exception,but adding null in TreeSet throw Exception

  23. 23

    Scala vs Java performance (HashSet and bigram generation)

  24. 24

    HashSet vs ArrayList CPU usage is high

  25. 25

    ArrayList vs HashSet, which is better for memory performance?

  26. 26

    What is increased cost of TreeSet vs LinkedHashSet and TreeMap over LinkedHashMap?

  27. 27

    List<T> vs HashSet<T> - dynamic collection choice is efficient or not?

  28. 28

    Which approach is faster when when searching for an element? HashSet vs .indexOf()

  29. 29

    C# Time complexity of Array[T].Contains(T item) vs HashSet<T>.Contains(T item)

HotTag

Archive