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




  • 1. Collection Interface



  • 2. List Interface

    1. ArrayList Class
    2. LinkedList Class
    3. Vector Class


  • 3. Set Interface

    1. HashSet Class
    2. TreeSet Class
    3. LinkedHashSet Class


  • 4. Map Interface

    1. HashMap Class
    2. TreeMap Class
    3. LinkedHashMap Class
    4. Hashtable Class


  • 5. Queue Interface

    1. Priority Queue Class
    Java.util.Collection hierarchy : Java Collections Framework Tutorial

    What is Java Collections Framework?
    Java Collections are similar to containers that consists of multiple items in a single unit. for e,g. collections of candies, list of names etc. Java Collections framework provides unified architecture for manipulating and representing Collections.

    Benefits of Java Collections Framework
    1. It improves program quality and speed.
    2. Increases the chances of reusability of software.
    3. Decreases programming effort.


    Java Collection Interfaces and Classes


    1. Collection Interface

    This is the root interfaces of the java collection classes hierarchy.A Collection represents a group of objects known as its elements.There is no direct implementation of Collection interface.It has very useful methods like size(),isEmpty(),contains(),add(),remove(),iterator() etc.

    2. List Interface

    List interface is an ordered collection meaning you can access the elements of a List in specific order. and by an index too. It can contain duplicate elements.Since List is an interface you need to provide a concrete implementation of the interface in order to use it.

    2.1 ArrayList Class

    ArrayList in Java tutorials are as follows:

    ArrayList Basics

    1. ArrayList in Java
    2. Initialize ArrayList
    3. Iterate (Loop) ArrayList
    4. Find Length of ArrayList

    ArrayList Sorting

    1. Sort ArrayList in Ascending Order
    2. Sort ArrayList in Descending Order
    3. Sort ArrayList of Objects using Comparable and Comparator

    ArrayList Add/Remove

    1. Add element to ArrayList
    2. Add element at Particular Index of ArrayList
    3. Append Collection elements to ArrayList
    4. Insert all the Collection elements at the Specified Position in ArrayList
    5. Remove element from Specified Index in ArrayList
    6. Remove specified element from ArrayList

    ArrayList Internal Working

    1. How Add() method works Internally in ArrayList

    ArrayList Conversions

    1. Convert HashSet to ArrayList in java
    2. Convert Array to ArrayList
    3. Convert LinkedList to ArrayList
    4. Convert ArrayList to String Array

    Differences

    1. ArrayList vs Array
    2. ArrayList vs Vector
    3. ArrayList vs LinkedList
    4. ArrayList vs HashMap
    5. ArrayList vs HashSet
    6. ArrayList vs CopyOnWriteArrayList

    Other Tutorials

    1. Synchronize ArrayList
    2. Serialize ArrayList
    3. How to Reverse ArrayList 

    2.2 LinkedList Class

    LinkedList in java tutorials are as follows :

    2.3 Vector Class

    Vector  is rarely used in the real world. It implements List interface.It maintains insertion order.It is synchronized that's why it gives poor performance in searching, adding, delete and update of the elements.

    1. Difference between Vector and ArrayList

    3. Set Interface

    Set interface stores unique elements meaning each element can only exists once in a Set.It is unordered meaning it makes no guarantee about the sequence of elements once you iterate them.Since Set is an interface you need to provide a concrete implementation of the interface in order to use it.

    3.1 HashSet Class

    3.2 TreeSet Class

    TreeSet in java tutorials are as follows:

    3.3 LinkedHashSet Class 

    LinkedHashSet implements Set interface. LinkedHashSet unlike HashSet maintains insertion order i.e elements are placed in the order they are inserted. LinkedHashSet should be preferred if you want a unique collection of objects in an insertion order.

    1. How LinkedHashSet works Internally in Java

    4. Map Interface

    Map interface represents a mapping between  a key and a value.The Map interface does not belongs to the Collection interface hierarchy, so,  it behaves a bit different from the rest of the collection types.Map is an interface you need to provide a concrete implementation of the interface in order to use it.

    4.1 HashMap Class

    HashMap in java tutorials are as follows:

    4.2 TreeMap Class

    TreeMap in java tutorials are  as follows:

    4.3 LinkedHashMap Class

    LinkedHashMap is a Hashtable and linked list implementation of the Map interface, with predictable iteration order. This class is different from HashMap and TreeMap i.e it maintains the insertion order. HashMap does not maintain any order and TreeMap sort the entries in ascending order of keys.

    1. How LinkedHashMap works Internally in Java

    4.4 Hashtable Class

    Hashtable implements hash table which maps keys to values. Any non null object can be used as  a key or as a value.

    1. Hashtable vs HashMap

    5. Queue Interface

    Queue  is an interface  you need to provide a concrete implementation of the interface in order to use  it. Queue typically but do not necessarily order elements in FIFO(first in first out) manner. Every Queue implementation must specify its ordering properties.

    5.1 PriorityQueue Class

    PriorityQueue class is based on a priority heap. The elements of the priority queue are ordered according to their natural ordering. A priority queue relying on natural ordering does not permit insertion of non-comparable objects.

    Other Tutorials in Java Collections

    Comparator vs Comparable
    Fail-fast Iterator vs Fail-safe iterator 

    About The Author

    Subham Mittal has worked in Oracle for 3 years.
    Enjoyed this post? Never miss out on future posts by subscribing JavaHungry