- Following are the important interfaces from Collections Framework- Collection, Set, SortedSet, List, Map, SortedMap, Queue
- Following are the important classes from Collections Framework- HashMap, Hashtable, TreeMap, LinkedHashMap, HashSet, LinkedHashSet, TreeSet, ArrayList, Vector, LinkedList, PriorityQueue, Collections, Arrays
- Note the distinction between Collection interface and Collections class. While former is the base or root interface for many other Collections Framework interfaces and classes like Set, List, Queue, the latter is a utility class containing a set of static utility methods.
- Note also that there is another (interface apart from Collection) Map which is base (or root) interface for other intreface and classes like HashTable, LinkedHashMap, HashMap, SortedMap, TreeMap.
- There are two utility classes in the Collections framework- Arrays and Collections, both of which extend from the Object class.
- Collections can be ordered or unordered and sorted or unsorted (though, with one exception, given next). A given collection can be either unordered and unsorted or ordered and sorted or ordered but unsorted. A collection can never be sorted but unordered.
- When a collection is ordered, it means you can iterate through the collection in a specific (non-random) order.
- When a collection is sorted, it means that the order in the collection is determined according to some rule or rules, known as the sort order.
- List interface maintians collections using indices. There are three implementations of List in the collections framework- ArrayList, Vector and LinkedList. ArrayList (or Vector) are preferred when faster iteration is required. LinkedList is preferred when fast insertion or deletion is required.
- Set interface in meant for holding collection of objects which does not have meaningful duplicates. The three implementation classes of Set interface are- HashSet (which is unordered and uses hashcodes for insertion and retrieval), LinkedHashSet (which maintains a doubly linked list across all elements of the set (so is ordered) ) and TreeSet (which is a set sorted by natural order of the objects)
- Map interface uses unique identifiers to store objects in the collection. There are three implementations of Map- HashMap (which is unordered and uses hashCode() of the key object to determine the value object's storage location), Hashtable (which is similar to HashMap but its method's are synchronized), LinkedHashMap (which maintains the insertion order of the objects) and TreeMap (which sorts kbjects by their natural sort order).
- HashMap and Hashtable have another difference (apart from Hashtable having synchronized methods). HashMap allows one null key and multiple null values, while Hashtable does not.
- Queue interface is designed to hold a list of things to do. The typical order in which objects are stored in a Queue is FIFO. Queue has one implementation class (apart from LinkedList)- PriorityQueue in which objects are ordered by their natural ordering. A Comparator interface can be used to specify custom ordering. In any case, the elements' ordering represents their relative priority.
- A LinkedList has faster (higher) addition-removal performance compared to a ArrayList. But it is slower when it comes to iteration. On the other hand, a LinkedHashMap has slower (lower) addition-removal performnance but faster iteration rate when compared with with a HashMap.
- Collections.sort() (one-arg) method takes an argument which is of List type and the objects in the List must implement an interface called Comparable.
- For implementing Comparable interface, a class has to implement the method compareTo(). This method (taking advantage of Generics in Java 5) takes an argument of the class type which is being compared.
- To sort classes in multiple different ways and to sort classes which you can not modify, one has to use Comparator interface. To implement Comparator interface, one has to implement the method compare() which takes two arguments of the type of the objects which are to be compared.
- Arrays of primitives are always sorted on their natural order.
- Arrays.sort() and Collections.sort() are static methods.
- Searching of Arrays and Collections is done using binarySearch method, successful search returns int index of the element being searched, unsuccessful search returns an int index that represents the insertion point of the element in the Array or Collection (the actual value returned is (-(insertion point) - 1).
- Arrays and collections must be sorted before sorted before it can be searched. It must be searched in the order in which it was sorted.
- Arrays.sort() and Collections.sort() methods update the objects being sorted.
- Iterators are used to iterate over elements of a Collection. Two important methods of an Iterator are "boolean hasNext()" and "Object next()".
- All the elements in a TreeSet and a TreeMap must be mutually comparable.
- String, Object and Enum classes override hashCode and equals() methods.
- Important methods of Queues interface are offer() (which loads elements in a Queue), peek() (which returns element at the head of the queue without removing it from the Queue), poll() (which returns the element at the head of the queue and removes it from the Queue.
No comments:
Post a Comment