TreeSet | HashSet | LinkedHashSet |
---|---|---|
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 values | unique values | unique values |
red-black tree | hash table | hash table with double links |
ascending order | undefined order | insertion 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
Good points. You can also check 5 difference between TreeSet and HashSet in Java for more differences.
ReplyDeleteThanks. Checked that and it's good!
Delete