Tuesday, August 31, 2010

Java Collections Framework


List<E>
Implementations: LinkedList<E>, ArrayList<E>, Vector<E>, Stack<E>

A List<E> is a collection which can contain duplicate elements, and the elements are ordered (like an array, you can add an element at a specific position, and the elements are accessed using their index).

[Stack<E> has come up in the examination in the past, so here is some information on it.

Stack<E> is a subset of Vector<E>, which contains some extra methods. The idea of a stack is like its name, it acts like a piled up stack of items: When you add an element, it goes to the top of the stack, and when you extract an element, it is taken off the top. In other words, this is a last-in, first-out system. The methods are:

push(object) - Add an element onto the top of the stack.
pop() - Removes the object at the top of this stack and returns that object as the value of this function.
peek() - Looks at (i.e. returns) the object at the top of this stack without removing it from the stack.. ]



Set<E>
Implementations: Hashset<E>

A Set<E> is a collection which cannot contain any duplicate elements and has no explicit order to its elements (unlike array, where every element exists at a particular index i.e. MyArray[15]).

SortedSet<E>
Implementations: TreeSet<E>

A SortedSet<E> is a Set<E> which maintains its elements in ascending order.



Map<K, V>
Implementations: HashMap<K, V>, Hashtable<K, V>, WeakHashMap<K, V>

Maps keys to values. In other words, for every key, there is a corresponding value, and you look up the values using the keys. Maps cannot have duplicate keys, and each key maps to at most one value.

Note: A Map<K, V> does not implement the Collection interface.

SortedMap<K, V>
Implementations: TreeMap<K, V>

A SortedMap<K, V> is a Map<K, V> which maintains its mapping in ascending key order.



add() method is available for any type of Collection classes only
TreeMap<K, V> is a java.util.Map<K, V>, use put() method with 2 arguments. This is due to Map<K, V> does not implement the Collection interface.




Thread-safe and slowest iteration speed
Hashtable, Vector

NON thread-safe
ArrayList, HashSet, HashMap, LinkedList, TreeSet, LinkedHashSet, TreeMap





Object Ordering

Implementations of the SortedSet and SortedMap interfaces sort their elements. To determine what criterion is used to sort the elements you can use either the Comparable or Comparator interfaces. Using Comparable means that the classes that you put in your SortedSet or SortedMap implement the Comparable interface, which just means that they contain a method compareTo() which determines whether the object is "greater" or "less than" another object. To use the Comparator interface, you pass an object which implements Comparator to your SortedSet or Sorted map, and it will use the Comparator for ordering.




Distinguish between correct and incorrect implementations of hashCode() methods.

The hashCode() value of an object gives a number which can be used to in effect to 'index' objects in a collection. A collection class can group its objects by their hashcodes, and if you called e.g. contains() (or get() in case of a Map), it can first find the group based on the hashcode, then search that group. This avoids the need to check every object in the collection.

The requirements of a hashCode() implementation are that if two objects are equal as determined by the equals() method, their hashcodes should be the same. This is why wherever you override the equals() method, you should override hashCode() as well.

The reverse is not required i.e. it is permitted for two objects that are not equal to have the same hashcode. This makes sense, as it is not always possible to ensure unique hashcodes. The method returns an integer and there are only an finite number of integer values to use. However, where possible, it is desirable to have the hashcodes be distinct as this can improve performance. If the hashcodes can be well distributed (i.e. scattered throughtout the integer range) as well, all the better.




BitSet

For completeness, here is some information on BitSet, which isn't part of the Collections Framework, but may be examinable:
A BitSet contains elements which are bits, i.e. of the boolean primitive type. Like, for example, a Vector, and unlike an array, a BitSet does not have a fixed size, and grows as needed when you add elements to it.






(^_^)





About

My photo
Petaling Jaya, Selangor, Malaysia