Collection framework consists of general purpose implementation of Set, Map, List interfaces.
Details of Interfaces in Collection framework is available at Collection Framework Interfaces
Main characteristics of General purpose implementation in Collection framework is described as below.
HashSet:
- Extends AbstractSet, implements Set, Cloneable, Serializable.
- Implements Set interface backed by HashMap instance.
- Null element is allowed.
- Unordered collection.
- Default initial capacity of 16 and load factor of 0.75.
- Constant time performances for basic operations (add, remove, size, contains).
- Iterating over set requires time proportional to number of elements in Set and capacity of backing HashMap instance.
- Not synchronized.
- Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
TreeSet:
- Extends AbstractSet, implements NavigableSet, Cloneable, Serializable
- Implementation is based on TreeMap.
- Elements are ordered by natural order or by Comparable provided at the time of creation of Set.
- Not synchronized.
- Null is not allowed.
- Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
- There are methods available to get subset of existing Set. Subset is backed by original Set, so any addition of element within range to set will be reflected to Subset and vice-versa.
- subset(fromElement, toElement) - fromElement is inclusive and toElement is excluded
- subSet(fromElement, boolean fromInclusive, toElement, boolean toInclusive) –using Boolean fromElement and toElement can be made inclusive or excluded.
ArrayList:
- Extends AbstractList, implements List, Cloneable, Serializable, RandomAccess.
- Random access of elements is fast compared to LinkedList.
- Element are stored in sequence in array.
- Null is allowed.
- Not synchronized.
- Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
- ArrayList has capacity which is size of array to store objects. ensureCapacity method is used to make sure that size of ArrayList is appropriate.
- Default initial capacity is 10.
LinkedList:
- Extends AbstractSequentialList, implements List, Deque, Cloneable, Serializable.
- Null is allowed.
- Methods available to add, remove, get element at the beginning and end of the list.
- Not synchronized.
- Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
HashMap:
- Extends AbstractMap, Implements Map, Serializable, Cloneable.
- HashMap allows one null key
- Not synchronized.
- Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
- HashMap has two parameters – Initial capacity and load factor which affects performance.
- Default initial capacity 16
- Default load factor 0.75
- Rehashing is done when capacity/load factor is more than number of elements in HashMap.