Free Guides

Language Tutorials

                            

SUN

Your Ad Here

 
               SUN CERTIFIED JAVA PROGRAMMER FOR JAVA2 PLATFORM 1.4

       Index     

Collections and Maps

Set

HashSet

TreeSet

LinkedHashSet


 

List

Vector

ArrayList

LinkedList


 

Map

HashMap

HashTable

TreeMap

LinkedHashMap

  • Set cannot contain duplicate elements. Not ordered (implementation : HashSet)
    SortedSet holds elements in sorted ascending order. (implementation : TreeSet)
    HashSet is not ordered or sorted. (constant time performance for basic add and remove).
    TreeSet arranges the elements in ascending element order, sorted according to the natural order of the elements.
    LinkedHashSet is an ordered HashSet, which gives elements in the order of insertion or last access.

  • List represents ordered collections which allow positional access and search and can contain duplicate elements. (implementation : LinkedList, ArrayList, Vector, Stack)
    ArrayList enables fast iteration ad constant speed positional access.
    Vector is similar to ArrayList only slower since it is synchronized.
    LinkedList allows fast insertion and deletion at the beginning or end. Commonly used for implementing stacks and queues.
    Stack is subset of Vector, it is simple LIFO.

  • Map matches unique keys to values. Each key maps to at most one value. It does not implement the Collection interface (implementation : HashMap, HashTable, WeakHashMap)
    SortedMap orders the mapping in the sorted order of keys.
    HashMap is not sorted or ordered. It allows one null key and many null values.
    HashTable is similar to HashMap but does not allow null keys and values. Also, it is slower because it is synchronized.
    LinkedHashMap iterated by insertion or last accessed order. Allows one null key and many null values.
    TreeMap is a map in ascending key order, sorted according to the natural order for the key's class.

  • Two objects that are not equal can have the same hash code. The hash code of a null value is defined as zero.

  • When you override equals() you should override hashCode()16 but the reverse is not required.

  • A BitSet is not part of the Collections Framework. It contains elements which are bits (ie. boolean primitive). Like Vector its not of fixed size and can grow as needed.