Iterable (Interface) : Abstractset (Abstract Class)
Iterable (Interface) : Abstractset (Abstract Class)
Top level interface extended by Collection. Allows Collection to be used in new foreach loop.
Collection (Interface)
- Group of objects.
- Methods that modify the collection are specified to throw UnsupportedOperationException.
- Unmodifiable collection.
- Most collections use .equals(Object o) for some of their methods like .contains(). But, they
can avoid it by say comparing hashCodes.
- It’s not possible for a collection to implement both Set and List. List.equals() contract says it
can be compared with only a List.
- Iterator doesn’t make sure the order in which elements are returned, unless the underlying
collection ensures that.
- Optional Operation: An operation on ineligible element that might result in adding the
element or throw an exception depending on the implementation.
- Cannot have primitive types. Autoboxing comes into the picture.
Set (Interface)
- HashSet (Class)
o Set backed by HashTable (a HashMap instance).
o Doesn’t ensure order of elements.
o Allows null elements.
o Constant time operations for add, remove, contains and size.
o Iterate over it take O(size of HashSet) + O(size of HashMap).
o Not synchronized. Can be made synchronized using
Collections.synchronizedSet() method.
o Iterator is fail-fast: If the set is modified after creation of itertor, it throws an
exception.
LinkedHashSet (Class)
• HashTable + LinkedList.
• Iteration order is fixed, the same in which elements were
inserted into the set.
• Performs worse than a HashSet.
• Iteration doesn’t depend on the capacity.
- SortedSet (Interface)
o Elements are sorted by natural ordering or by using a Comparator (provided
at creation time).
TreeSet (Class)
• Based on a TreeMap.
• Log(n) for basic operations – add, remove, contains.
• Not synchronized and iterator fail-fast (same as HashSet).
List (Interface)
- An ordered collection.
- Search by index.
- Allows duplicates.
- Allows null (some may not).
- Special iterator: ListIterator. Provides insertion and modification and bidirectional access in
addition to what Iterator provides.
- ArrayList (Class)
o Permits duplicates and null elements.
o Unsynchronized form of Vector.
o Contains uses object’s equal method
- LinkedList (Class)
o Permits null and duplicates.
o Can be used as stack, queue, or double-ended queue.
o Implements Deque interface.
o Contains uses object’s equal method
Map (Interface)
- HashMap (class)
o HashTable implementation of Map.
o Permits null values and key.
o Comparison with HashTable – unsynchronized and permits null.
o Constant time operations – add, remove, etc.
o Iterator time = size + capacity(map);
o Load Factor: How full the hash table is allowed to get before its capacity is
automatically increased.
- LinkedHashMap (Class)
• Properties similar to LinkedHashSet.
- SortedMap (Interface)
o Properties similar to SortedSet.
- TreeMap (Class)
• Properties similar to TreeSet.
- ConcurrentHashMap (Class)
o Thread-safe version.
- IdentityHashMap (Class)
o Reference-based equality instead of object-equality when comparing keys and
values (k1 == k2 instead of k1.equals(k2)).
o Used rarely.
- WeakHashMap (Class)
o Map implementation with weak keys.
o Entry will be removed when found weak by the GC.
o Over time, size is reduced as if some external thread is removing the entries.