Collection Classes | Duplicates Accepted? | Insertion Order maintained? | Sorted Order? | Is Thread Safe? |
---|---|---|---|---|
List | ||||
ArrayList | Yes | Yes | No | No |
LinkeList | Yes | Yes | No | No |
Vector | Yes | Yes | No | Yes |
Set | ||||
HashSet | No | No | No | No |
LinkedHashSet | No | Yes | No | No |
TreeSet | No | Yes | Yes | No |
Map | ||||
HashMap | No | No | No | No |
LinkedHashMap | No | Yes | No | No |
Hashtable | No | No | No | Yes |
TreeMap | No | Yes | Yes | No |
- All lists allow duplicate elements and follow insertion order.
- All list elements are not sorted.
- All sets and maps do not allow duplicate elements.
- Sets and maps do not sort their elements, except TreeSet and TreeMap โ in which elements are sorted by natural order or by a comparator.
- Elements within sets and maps are not ordered, except for:
- LinkedHashSet and LinkedHashMap have elements ordered by insertion order.
- TreeSet and TreeMap have elements ordered by natural order or by a comparator.
- There are only two collections that are thread-safe: Vector and Hashtable. The rest is not thread-safe.
Hope you have learned something new. If yes, share it with your friends and colleagues.
Let's meet at the other tech article next time. Until then stay connected with me on LinkedIn
-- Pasumarthi Sai Phani Krishna (aka PSPK)๐