Wednesday, November 2, 2011

TreeSet vs HashSet vs LinkedHashSet

The post here nicely summarized the collections TreeSet, HashSet, and LinkedHashSet in Java. I mostly repeat that.

TreeSetHashSetLinkedHashSet
public class TreeSet
extends AbstractSet
implements SortedSet, Cloneable, Serializable
public class HashSet
extends AbstractSet
implements Set, Cloneable, Serializable
public class LinkedHashSet
extends HashSet
implements Set, Cloneable, Serializable
unique valuesunique valuesunique values
red-black treehash tablehash table with double links
ascending orderundefined orderinsertion order
\(O(\log n)\) for add, remove and contains\(O(1)\)\(O(1)\), a littler slower than HashSet
except for the operation of iteration

For more details, please see the reference blog.

References:
Vidya's Blog

2 comments: