I got a chance to discuss our very old Java collections with a group of guys today. We went through various collections and tried to play with them. It was exciting and brought back memories of my Java days. I thought I would just list down some beaten-to-death points about the these collections we discussed today.
- ArrayList allows duplication
- ArrayList retains the insertion order
- ArrayList is slower in terms add/remove compared to LinkedList
- Set does not allow duplication;
- But checks if the item is duplicate using the hashCode and equals method of the item
- HashSet sorts based on mod(16) ie., the initial capacity
- TreeSet is a sorted collection
- Each item should implement Comparable interface for the sorting order.
- HashMap does not allow duplication
- No guarantee on sorting/ordering of the keys;depends on the mod(capacity)
- Hashtable and HashMap are very similar, one of them is synchronized and other than is not synchronized
- LinkedHashMap does not allow duplication
- Maintains the insertion order
- No sorting
- TreeMap does not allow duplication
- Sorts the keys
- Need to implement Comparable interface for specifying the sorting algorithm
- Runtime Exception if the item does not implement Comparable interface