Java Collections Framework Tutorial

Collections in java are used in almost every enterprise application. Java Collections framework consists of interfaces and classes which helps in storing and processing the data efficiently. Java Collections Tutorial will look into each of the classes.

Read Also :  Collections in Java Interview Questions 

Java Collections Interfaces and Classes


  • Collection Interface
  • HashSet Class in Java Tutorial with Example

    Java HashSet class implements Set interface. It has the following properties.

    1. HashSet does not allow duplicates, means it contains unique elements.
    2. HashSet is unordered meaning it does not guarantee that the order will remain constant over time.
    3. HashSet is backed by Hashtable (actually a HashMap instance).
    4. HashSet permits the null elements.
    5. HashSet implementation is not synchronized. It can be synchronized externally.
        Set s = Collections.synchronizedSet(new HashSet(...));
    6. Iterators returned by HashSet class is fail- fast.

    HashSet Example

    TreeMap Class in Java Tutorial with Example

    TreeMap class implements the Map interface.It extends AbstractMap class and implements NavigableMap interface. It has the following properties:

    1. TreeMap sorts the keys according to the natural ordering of its keys, or by Comparator provided at  map creation time.
    2. TreeMap is a red black tree based NavigableMap implementation.
    3. TreeMap implementation provides guaranteed log(n) time cost for the containsKey,get,put and remove operations.
    4. TreeMap does not allow null keys and null values.
    5. TreeMap is not synchronized.It must be synchronized externally
        SortedMap  m = Collections.synchronizedSortedMap(new TreeMap(...));

    TreeMap Example


    TreeSet Class in Java Tutorial with Example

    TreeSet class implements Set interface. It extends AbstractSet class and implements NavigableSet interface.It has the following properties:

    1. TreeSet implements Set interface so it does not allow duplicate values.
    2. TreeSet sorts the elements in the ascending order.
    3. TreeSet does not allow to insert heterogeneous elements. ClassCastException will be thrown if you try to add a heterogeneous element.
    4. TreeSet doesn't allow to add null element.
    5. TreeSet implementation is not synchronized. It can be synchronized explicitly by using
        SortedSet  s = Collections.synchronizedSortedSet(new TreeSet(...);
    
    


    TreeSet Example


    LinkedList Class in Java Tutorial with Example

    Java LinkedList implements the List interface. It has the following properties:

    1. LinkedList is a Doubly-linked list implementation of the List and Deque interfaces.
    2. LinkedList can contain duplicate elements.
    3. LinkedList class maintains insertion order.
    4. LinkedList class manipulation is fast because no shifting needs to be occurred.
    5. Java LinkedList class is unsynchronized.
    6. Java LinkedList class can act as a stack, queue or list.

    LinkedList Example