Wednesday, April 14, 2010

Collection


List (interface)





Order is the most important feature of a List; it promises to maintain elements in a particular
sequence. List adds a number of methods to Collection that allow insertion and removal of elements in the middle of a List. (This is recommended only for a LinkedList.) A List will produce a ListIterator, and using this you can traverse the List in both directions, as well as insert and remove elements in the middle of the List.

ArrayList:

 A List implemented with an array. Allows rapid random access to elements, but is slow when inserting and removing elements from the middle of a list. ListIterator should be used only for backand-forth traversal of an ArrayList, but not for inserting and removing elements, which is
expensive compared to LinkedList.

LinkedList :
Provides optimal sequential access, with inexpensive insertions and deletions from the middle of the List. Relatively slow for random access. (Use ArrayList instead.) Also has addFirst( ), addLast( ), getFirst( ), getLast( ), removeFirst( ), and removeLast( ) (which are not defined in any interfaces or base classes) to allow it to be used as a stack, a queue, and a deque.

Set (interface) :

Each element that you add to the Set must be unique; otherwise the Set doesn’t add the duplicate element. Objects added to a Set must define equals( ) to establish object uniqueness. Set has exactly the same interface as Collection. The Set interface does not guarantee it will maintain its elements in any particular order.

HashSet:  For Sets where fast lookup time is important. Objects must also define hashCode( ).

TreeSet:  An ordered Set backed by a tree. This way, you can extract an ordered sequence from a Set.
LinkedHashSet:  Has the lookup speed of a HashSet, but maintains the order that you add the elements (the insertion order), internally using a linked list. Thus, when you iterate through the Set, the results appear in insertion order.

Map (interface) : Maintains key-value associations (pairs), so you can look up a value using a key.

HashMap: Implementation based on a hash table. (Use this instead of Hashtable.) Provides constant-time performance for inserting and locating pairs. Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table.

LinkedHashMap:  Like a HashMap, but when you iterate through it you get the pairs in insertion
order, or in least-recently-used (LRU) order. Only slightly slower than a HashMap, except when iterating, where it is faster due to the linked list used to maintain the internal ordering.

TreeMap:  Implementation based on a red-black tree. When you view the keys or the pairs, they

0 comments: