Java Program that Shows use of Collection Framework


In java, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. The collection interface is present at the root of all collection framework interfaces. We can perform various operations on collection like adding, removing, iterating, searching and retrieving objects. Note that they can’t work with primitive datatypes like int, double. However, java provides wrapper classes that enable the use of primitive datatypes as object. We will use those objects to work with collection interface.

In this article, we will make a java program that shows the use of Collection Interface. We will also discuss few sub interfaces and methods of collection interface.

Collection Interface

Hierarchy of Collection Interfaces

Syntax

Collection<element_Type> collection_name = new Collection<element_Type>();
Or,
Collection<element_Type> collection_name = new Collection<>();

Instance

ArrayList<Integer> al = new ArrayList<>();

‘al’ is the name of array list collection_name.

‘Integer’ is the type of element that we are going to store. Note here, it is an object not primitive.

‘ArrayList’ is the Collection.

It is important to import the ‘java.util’ package because collection interfaces are available in this package. To import use the following command −

import java.util.*;

Here, * signifies that we are importing all the classes as well as methods available in this package into our program.

Let’s discuss the sub interfaces of collection interface −

List − It is the sub interface of java Collection Interface. It is a linear structure that stores and accesses each element in a sequential manner. To use the features of list, we will use ArrayList and LinekdList class that implements list interface.

Set − It is the sub interface of java Collection Interface that doesn’t allow duplicate values. It is similar to mathematical set. To use the features of set, we will use hash set class that implements set interface.

Queue − It provides the features of queue data structure. Queue follows the First in First out (FIFO) approach.

some inbuilt methods that we will use in our program −

  • add() − it is used to add a single element at the end of list.

  • hasNext() − it returns true if element is present otherwise false.

  • next() − it display the next element of the list.

  • iterator() − It is used to iterate through list.

  • get() − it returns elements of specified index.

  • size() − it returns the number of elements in the list.

Program to show the use of Colllection Interface

Example 1

The following program illustrates the use of collection interface −

import java.util.*;
public class Listcol {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the Arraylist : " + araylist);
      // Creating Linkedlist 
      LinkedList<Integer> list2 = new LinkedList<>();
      // Adding elements in linkedlist
      list2.add(8);
      list2.add(4);
      list2.add(1);
      list2.add(0);
      System.out.println("Elements of the Linkedlist : " + list2);
      // Creating Set
      HashSet<Integer> set = new HashSet<>();
      // Adding elements in Hashset
      set.add(9);
      set.add(6);
      set.add(1);
      set.add(3);
      System.out.println("Elements of the HashSet : " + set);
      // Creating Queue
      Queue<String> queue = new PriorityQueue<>();
      // Adding elements in queue
      queue.add("Tutorix");
      queue.add("Tutorialspoint");
      queue.add("Tutorial");
      System.out.println("Elements of the Queue : " + queue);
   }
}

Output

Elements of the Arraylist : [8, 5, 2, 9, 4, 7]
Elements of the Linkedlist : [8, 4, 1, 0]
Elements of the HashSet : [1, 3, 6, 9]
Elements of the Queue : [Tutorial, Tutorix, Tutorialspoint]

Example 2

Let’s see how we can iterate through the elements in the list using for loop.

import java.util.*;
public class RunningSum {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // loop to iterate through elements
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements in the list
         System.out.println(araylist.get(i)); 
      }
   }
}

Output

Elements of the list : 
8
5
2
9
4
7

Example 3

In this example, we will see how we can iterate through elements of list using Iterator Interface.

import java.util.*;
public class Listcol {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // creating iterator interface to iterate through elements
      Iterator<Integer> itr = araylist.iterator();
      while (itr.hasNext()) {
         // to print the elements in the list
         System.out.println(itr.next()); 
      }
   }
}

Output

Elements of the list : 
8
5
2
9
4
7

Conclusion

In this article, we have discussed the classes and methods that implement different collection interfaces. We have seen program to understand the use of these interfaces. They are mainly used to group objects. Also, we discussed about iterator interface.

Updated on: 02-May-2023

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements