Java Collection Hierarchy

The collection is a framework in java. It contains some of the most used classes which are present in the interfaces. Collection interface mainly has 3 child interfaces and these are as follows.

Java Collection Hierarchy

Image Source

1. List

It contains an order of all the objects. This interface also helps you to have a dynamic size of arrays according to the number of objects present. It is implemented by Vector, Stack, ArrayList, and LinkedList. Therefore we can create a list object with these classes.

import java.util.*;
public class list {

	public static void main(String[] args) {
		List<Integer> mylist=new ArrayList();
		// Instead of ArrayList we can write LinkedList, Vector or Stack
	}

}

ArrayList

Arraylist is a kind of dynamic array that stores values of all data types. It costs constant amortized time to add an element in ArrayList. Let’s look at the code to see different methods in ArrayList.

import java.util.*;
public class arraylistfunctions {

	public static void main(String[] args) {
		ArrayList<Integer> arrlist=new ArrayList();
		Scanner sc=new Scanner(System.in);
		int option;
		do{
			System.out.println("1. Enter a new integer value to add in the arraylist");
			System.out.println("2. Enter the index on which the element you want to be removed");
			System.out.println("3. Find the size of the Arraylist");
			System.out.println("4. Enter the index of the element you want to be printed");
			System.out.println("5. Exit");
			System.out.println("Please write an option: ");
			option=sc.nextInt();
			switch(option) {
			case(1):
				System.out.println("Please Enter the value to add: ");
				int temp=sc.nextInt();
				arrlist.add(temp);
				break;
			case(2):
				System.out.println("Please Enter the index of value to remove it");
				int ind=sc.nextInt();
				arrlist.remove(ind);
				break;
			case(3):
				System.out.println("The size of Arraylist is: "+arrlist.size());
				break;
			case(4):
				System.out.println("Enter the index: ");
				int index=sc.nextInt();
				System.out.println("The value is: "+ arrlist.get(index));
				break;
			case(5):
				break;
			}
		}while(option!=5);
		
		

	}

}

LinkedList

It uses a doubly linked list while storing the values, so getting the first & last value takes constant time. As compared to ArrayList the manipulation is faster because of storing data in the doubly linked list. Its methods are quite similar to the ArrayList.

Vector

Vector also works as a dynamic array. It is advised to use vector in thread-safe implementations otherwise we should use ArrayList in all other cases. Let’s look at the below code to see its functions.

import java.util.*;
public class vectorfunctions {

	public static void main(String[] args) {
		Vector<Integer> myvec=new Vector();
		Scanner sc=new Scanner(System.in);
		int option;

		do{
			System.out.println("1. Enter a new integer value to add in the Vector");
			System.out.println("2. Enter the index on which the element you want to be removed");
			System.out.println("3. Find the size of the Vector");
			System.out.println("4. Enter the index of the element you want to be printed");
			System.out.println("5. Printing the whole vector");
			System.out.println("6. Exit");
			System.out.println("Please write an option: ");
			option=sc.nextInt();
			switch(option) {

			case(1):

				System.out.println("Please Enter the value to add: ");
				int temp=sc.nextInt();
				myvec.add(temp);
				break;

			case(2):

				System.out.println("Please Enter the index of value to remove it");
				int ind=sc.nextInt();
				myvec.remove(ind);
				break;

			case(3):

				System.out.println("The size of Vector is: "+myvec.size());
				break;

			case(4):

				System.out.println("Enter the index: ");
				int index=sc.nextInt();
				System.out.println("The value is: "+ myvec.get(index));
				break;

			case(5):

				System.out.println("The vector is as follows: "+myvec);
				break;

			case(6):

				break;

			}
		}while(option!=6);
		

	}

}

Stack

Stack arranges the values by the last in the first out (LIFO) method. We can add elements by the push method and remove them by the pop method. The way of creating a stack is as follows.

import java.util.*;
public class stackfunctions {

	public static void main(String[] args) {
		Stack<Integer> mystk=new Stack();
		Scanner sc=new Scanner(System.in);
		int option;
		do{
			System.out.println("1. Enter a new integer value to add in the stack");
			System.out.println("2. Removing the item at the top of the stack");
			System.out.println("3. Find the size of the stack");
			System.out.println("4. Printing the whole stack");
			System.out.println("5. Exit");
			System.out.println("Please write an option: ");
			option=sc.nextInt();
			switch(option) {
			case(1):

				System.out.println("Please Enter the value to add: ");
				int temp=sc.nextInt();
				mystk.push(temp);
				break;

			case(2):

				int t=mystk.pop();
			        System.out.println("The value "+t+" has been removed successfully from the stack.");
				break;

			case(3):

				System.out.println("The size of Stack is: "+mystk.size());
				break;

			case(4):

				System.out.println("The stack is as follows: "+mystk);
				break;

			case(5):

				break;

			}
		}while(option!=5);

	}

}

2. Queue

It contains the objects in a specific order in which they are added to the queue. If the queue is empty initially and we add a value then it will be at the starting of the queue as long as the queue exists. This method is also known as First In First Out (FIFO). It is implemented by Deque, PriorityQueue, ArrayDeque.

import java.util.*;
public class queuefunctions {

	public static void main(String[] args) {
		Queue<Integer> myqueue=new LinkedList<>(); 
		Scanner sc=new Scanner(System.in);
		int option;
		do{
			System.out.println("1. Enter a new integer value to add in the queue");
			System.out.println("2. Removing the top element in the queue ");
			System.out.println("3. Find the size of the queue");
			System.out.println("4. Printing the whole queue");
			System.out.println("5. Exit");
			System.out.println("Please write an option: ");
			option=sc.nextInt();
			switch(option) {
			case(1):

				System.out.println("Please Enter the value to add: ");
				int temp=sc.nextInt();
				myqueue.add(temp);
				break;

			case(2):

				myqueue.remove();
				break;

			case(3):

				System.out.println("The size of Queue is: "+myqueue.size());
				break;

			case(4):

				System.out.println("The queue is as follows: "+myqueue);
				break;

			case(5):

				break;

			}
		}while(option!=5);
		
	}

}

3. Set

It is a collection of objects which doesn’t allow the same values in the same set i.e, each value will be different in a set. So whenever we need unique values we use them to set in our programs. It is implemented by TreeSet, LinkedHastSet, and many more. We can create set objects with these classes.

import java.util.*;
public class setfunctions {

	public static void main(String[] args) {
		 Set<Integer> myset = new HashSet<Integer>();
		 Scanner sc=new Scanner(System.in);
		 int option;
		 do{
				System.out.println("1. Enter a new integer value to add in the set");
				System.out.println("2. Enter the index on which the element you want to be removed");
				System.out.println("3. Find the size of the set");
				System.out.println("4. Printing the whole set");
				System.out.println("5. Exit");
				System.out.println("Please write an option: ");
				option=sc.nextInt();
				switch(option) {
				case(1):

					System.out.println("Please Enter the value to add: ");
					int temp=sc.nextInt();
					myset.add(temp);
					break;

				case(2):

					System.out.println("Please Enter the value you want to remove ");
					int ind=sc.nextInt();
					myset.remove(ind);
					break;

				case(3):

					System.out.println("The size of sett is: "+myset.size());
					break;

				case(4):

					System.out.println("The elements of the set are: "+myset);
					break;

				case(5):

					break;

				}
			}while(option!=5);
	}

}

This was brief information about the collection hierarchy in Java. Comment down below if you have any queries.

Leave a Comment

Your email address will not be published. Required fields are marked *