Collection Framework in Java

Hi Friends, In this tutorial we will discuss about Collection Framework In Java, different classes and Interfaces which are inside the collection framework, methods of List and will see sample code of List and ArrayList.

 

What is Collection in Java?

In single line we can say collection is a group of objects. In collection to represent the group of objects into single entity we need set of classes and interfaces.

Java Collection Framework provides many classes(ArrayList, LinkedList, Vector etc.) and Interfaces (List, Set, Queue, Deque).

 

Why Collection Framework in Java?

Without collection we can store the objects into single unit but there is some limitations without collection. For example if we will store data using array then we can store only homogeneous data type. If we create array with int type then we can store only integer data type.

Other limitation is array size is always fix. We cannot change the size at runtime.

To overcome above limitations we have Collection in Java.

 

What is Collections?

Collections is a class which is present in java.util package. Collections contain the methods which are used in Collection to perform certain operations.

 

Hierarchy of Collection Framework:

In below image we can see Collection framework’s classes and Interfaces. The java.util package contains all classes and interfaces.

Hierarchy of Collection Framework

 

We will discuss one by one about above mentioned Interfaces and Classes.

 

List: List is an interface which is child interface of Collection interface. It will preserve insertion order of objects. It can contain duplicate elements.

List interface is implemented by ArrayList, LinkedList and Vector class.

Set: Set is an interface which is child interface of Collection interface. It will not preserve insertion order of objects. It cannot contain duplicate elements.

Set interface is implemented by HashSet and LinkedHashSet class.

Queue: Queue is an interface which is child interface of Collection interface. It maintains the First-in First-out order. It is used to hold the elements which are about to processed.

Queue Interface is implemented by PriorityQueue.

Queue Interface is also extended by Deque interface and Deque interface is implemented by ArrayDeque.


Now we will discuss methods of Collection interface and also will discuss methods of List, Set and Queue Interface.

Methods of Collection Interface

  • add(object O) – It is used to add an element in the collection.
  • addAll(Collection C) – It is used to add specified collection elements in the different collection.
  • remove(object O) – It is used to remove an element in the collection.
  • removeAll(Collection C)- It is used to remove all specified collection elements from the different collection.
  • retainAll(Collection C) – It is used to remove all elements from invoking element except specified collection elements.
  • Clear()- It is used to clear all the object from the collection.
  • IsEmpty()- It is used to check the collection is empty or not. It will return Boolean value true or false.
  • contains(object O) – It is used to check the presence of particular element in collection.
  • containsAll(Collection C) – It is used to check presence of all elements of specified collection in invoking collection.
  • toArray(Collection C) – It is used to convert specified collection into array of objects.

All above Collection methods are common and present into List, Set and Queue.

List, Set and Queue have also their own methods. Now we will discuss those methods.


Methods of List Interface

  • add(index, object o) – It is used to add elements  at the specified position/index of the list.
  • addAll(index, Collection C) – It is used to add all elements of specified collection in starting of specified position of the list.
  • remove(index) – It is used to remove element from the specified position of the list.
  • get(index) – It is used to get the element from the specified position of the list.
  • set(index, Object O)- It is used to set the specified object at given index in the list.

 

ArrayList: It is a class which implemented the all methods of List interface. It is a dynamic array to store the duplicate elements of different data types. This class also maintains the insertion order.

Sample Code of ArrayList:

package DemoPkg;


import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExp {

    public static void main(String[] args) {


        ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
        list.add("Rachel");//Adding object in arraylist  
        list.add("Avi");  
        list.add("John");  
        list.add("Nick"); 
        
        //Traversing list through Iterator 
        
        Iterator itr=list.iterator();  
        while(itr.hasNext()){  
        System.out.println(itr.next());  
        }  
    }

}

Output:

ArrayList output

 

Sample Code of Add, Remove, Size, Get, isEmpty methods of ArrayList:

package DemoPkg;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExp {

    public static void main(String[] args) {


        ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
        list.add("Rachel");//Adding object in arraylist  
        list.add("Avi");  
        list.add("John");  
        list.add("Nick"); 
        
        
        System.out.println(list.size()); // 4
        System.out.println("Get element of index 1: "+list.get(1)); // Avi
        list.remove(1); // It will remove Avi
        System.out.println("After remove one element size is: " + list.size()); // 3
    
        // Set will replace element from specified position
        list.set(2, "Ajeet");
        
        System.out.println("After set element at index 2, List elements are:- ");
        //Traversing list through Iterator 
        Iterator itr=list.iterator();  
        while(itr.hasNext()){  
        System.out.println(itr.next());
        }  
        
        System.out.println("Check whether the list is empty or not: "+list.isEmpty());
    }

}


Output:

 

Note: If we want to sort the list elements then we will use sort() method of collections class

Sample Code of Sorting ArrayList elements:

package DemoPkg;

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListExp2 {
    
    
    public static void main(String[] args) {
        
        ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
        list.add("Rachel");//Adding object in arraylist  
        list.add("Avi");  
        list.add("John");  
        list.add("Nick");
        
        System.out.println("ArrayList before sorting:" + list);
        
        // Sorting list element using sort() method
        Collections.sort(list);
        
        System.out.println("ArrayList after sorting:" + list);
        
        //shuffle rearrange the list elements
        Collections.shuffle(list);
        
        System.out.println("ArrayList after Suffling:" +list);
    }

}

Output:

ArrayList before sorting:[Rachel, Avi, John, Nick]

ArrayList after sorting:[Avi, John, Nick, Rachel]

ArrayList after Suffling:[Rachel, Nick, Avi, John]

 

I hope you have enjoyed this tutorial. In this we have covered Collection in java, Hierarchy of Collection Framework, Different classes and Interfaces of Collection interface, Methods of List Interface and Sample code of ArrayList.

In coming tutorial we will cover other topics like Set, Queue etc.

Happy Learning 🙂

 

Must Read

Lambda Expression in Java

Functional Interface in Java 8

Leave a Comment