Thursday, January 6, 2011

Comparing three implementations of Set interface

Comparing three implementations of Set interface 

Classes implementing Set

Underlying data structure

Pros

Cons

HashSet

hash-table

1.        Useful when you want to impose only uniqueness among elements.

2.        Faster than other Set implementation as it does not need to maintain any order.

1.        Do not guarantee any order among elements.

2.        Not thread –safe.*

LinkedHashSet

hash-table

and

doubly linked list

1.        Maintains the insertion order.

1.        Slower than HashSet as it maintains the insertion order.

2.        Not thread –safe.*

TreeSet

tree

1.        Maintains the natural order among elements.

2.        You can impose different order by passing your own Comparator.

1.        Not thread –safe.*

No comments:

Post a Comment