What are the differences between TreeSet, HashSet and LinkedHashSet?

10

The idea of Set is not to allow repeated elements, in Java there are three implementations TreeSet , HashSet and LinkedHashSet found some differences In this answer , I would like to know in detail if there are others and in which case / scenario each collection stands out.

    
asked by anonymous 15.06.2018 / 17:12

1 answer

9

What changes are the inner structures. Some give more guarantees than others.

I will not go into details of what a Set does, does not seem to be the purpose of the question.

TreeSet

Only applies to elements with absolute ordering. If they are not comparable ( Comparable ), you can teach with a comparator ( Comparator ).

Underneath the cloths, use a balanced search tree (I think binary tree, red-black, but I do not remember the source of this information or if the internal details have already changed).

Since it is a balanced tree, all operations are done in O(lg(n)) . You have guarantees that you will iterate differently than the order in which the elements were inserted.

HashSet

Below, it uses a spreadsheet that points to a linked list of nodes. When there is a collision, it is inserted at the beginning of the linked list.

Only applicable to objects that have hash function. In Java, all objects have this function ( Object.hashCode() ), then all are candidates for this collection = D

  

If you want to use a spreading number of your own, as if you supplied a IntFunction<T> to generate the number, then you need to create your own collection or make a HashSet<MyHashWrapper<T>> , where MyHashWrapper<T> would be a wrapper that contains a object T and a function of hash itself.

In the ideal world, operations in O(1) , but can degrade to O(n) .

There is no guarantee of iteration order.

LinkedHashSet

Same as HashSet , but the nodes in the linked collision list have an extra link. This binding ensures that it is possible to iterate in the insertion order. Hence, you have the guarantee that iterates in the same order of insertion.

When to use each?

In general, the HashSet you would use for laziness. Reasonably good performance in most cases. It is difficult to access in the same order as the insertion.

The LinkedHashSet cost two cents of extra memory and one of processing, should be used to extract in order of insertion.

While TreeMap would have two possible uses:

  • avoid degradation of hash
  • Always iterate in ascending order

If your application needs to go through these many and many times in ascending order, then it may make sense to use TreeMap for this purpose. If you do not have to do this iteration constantly, but only from time to time, transforming from Set to List and sorting will not be a big overhead . Hence laziness speaks louder and I would use HashSet =)

    
15.06.2018 / 17:40