SCJP Study Notes
The Collections Framework
- HashMap can store one null key and multiple null values but HashTable can not have any null.
- HashTable is synchronized but HashMap is NOT.
- Set can not contain duplicate elements.
- Map does not extend Collection interface.
- The classes that implements Map can not contain duplicate keys.
- ArrayList: Ordered by index, fast iteration but slow if there are a lot of insertion/delete.
- Vector: Threadsafe, fast iteration but slower than ArrayList.
- ArrayList and Vector both use an array to store the elements of the list; so access to any element using an index is very fast. A LinkedList is implemented using
a doubly linked list; so access to an element requires the list to be traversed using the links. - When you iterate over a TreeSet, elements will always be presented in sorted order. If the iteration dos not matter, the higher performance HashSet can be used.
- The Set interface extends Collection but does not add any additional methods.
- Stack extends Vector, adding push(), pop(). peek() methods to support stack behaviour.
- The java.util package contains two support classes, called Collections and Arrays. These provide static methods that operate an collections and arrays.
- Methods of Collection interface: add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray.
- Methods of List interface: get, indexOf, set. (extends Collection)
- Methods of SortedSet interface: first, last, subSet, tailSet. (extends Set)
- Methods of Map interface: clear, containsKey, containsValue, get, isEmpty, keySet, put, remove, size, values.
- Methods of SortedMap: firstKey, lastKey, subMap, tailMap. (extends Map)
- Static methods of support class Collections: disjoint, frequency, max, min, reverse, shuffle, sort.
- Static methods of support class Arrays: asList, sort, binarySearch, equals.
- HashMap and Hashtable do not implement the Collection interface. Instead, HashMap extends AbstractMap and implements the Map interface.
Hashtable extends Dictionary and implements the Map interface. - The HashMap, HashSet and Hashtable classes are all implemented with an internal hashtable that organizes the elements in buckets
according to the hashcode and not according to the order of insertion. - The LinkedHashMap and LinkedHashSet classes also use an internal hashtable, and both also maintain a linked list through all of the elements.
The order of the list is determined by the order of insertion. - The Enumeration interface was introduced with Java 1.0 to provide an easy means of moving through the elements of a Vector or the keys or values of a Hashtable.
- The Iterator interface was introduced with the collections framework with Java 1.2. The Iterator interface declares three methods: hasNext, next and remove.
- The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities,
List modification capabilities and the ability to determine the position of the iterator in the List. The ListIterator interface does not extend the List interface. - The LinkedList class provides methods such as addFirst, addLast, getFirst, getLast, removeFirst and removeLast that facilitate the implementation of stacks and queues.
