Java Collections Framework with Code Examples


The Java Collections Framework is a core part of the Java programming language that offers a wide range of data structures and methods for maintaining and manipulating collections of objects. It is a crucial tool for Java developers since it provides a standardized method for storing, accessing, and processing data. Lists, sets, and maps, among other collection types with their own distinctive qualities, are all included in the Java Collections Framework. It offers numerous ways to organize and modify groups of items, including searching, sorting, data addition and deletion.

 

Java Collection

Java Collections are objects that store groups of other objects. For Java developers, they are an effective tool since they provide a standardized method for handling and manipulating data. Typical Java Collection examples include linked lists, stacks, and queues, which can be used to model actual objects like trains, queues, and browser histories.

 

Java Collection Frameworks

The Java Collections Framework is a robust tool for effectively handling and manipulating groups of objects. It offers a set of methods for handling and modifying data, which makes it simpler to create Java applications. A number of collection types, including lists, sets, and maps, are included in the Java Collections Framework. Each has distinctive qualities of its own. It offers numerous ways to modify collections of items, including searching, sorting, data addition, deletion, and updating.

 

Hierarchy of Collection Framework

Collection Interface

The Java Collections Framework offers a consistent design for displaying and working with collections of objects, with the Collection interface acting as the root interface of the hierarchy. It is declared as:

 

public interface Collection<E>

The parameter E specifies about the object that the collection can contain.

 

Methods of Collection Interface

The collections interface offers a various number of methods to manage the objects:

●           add() : adds a certain element to a collection

●           size() :returns the number of elements in the collection

●           remove() :removes a certain element from the collection

●           clear() :removes all elements from the collection

●           Iterator() :returns an iterator to access elements of collection

●           contains() :returns true if specified element is in the collection otherwise returns false

 

Subinterfaces of Collection

The collection interface includes various subinterfaces that are implemented by different java classes. The components include List,Queue and Set.

 

  1. List Interface:

 

The Java List interface extends the Collection interface and provides a way to store ordered collections of data. Lists can contain duplicate elements, and the order of elements is preserved. Many classes, including LinkedList, ArrayList, Vectors, and Stack, implement lists.When it comes to organising ordered collections of data in Java, lists are a potent tool that can be applied to a wide range of programming issues.

 

Example :Implementing the ArrayList

import java.util.ArrayList;
import java.util.List;
 
public class ListCollectionDemo{
	
	public static void main(String[] args) {

		// Create a new ArrayList
		List<String> list = new ArrayList<>();
		 
		// Add some elements to the list
		list.add("Apple");
		list.add("Mango");
		list.add("Pear");
		 
		// Print the list
		System.out.println(list);
		 
		// Get the element at index 1
		String element = list.get(1);
		 
		// Remove the element at index 2
		list.remove(2);
		 
		// Print the list again
		System.out.println(list);
	}
}

 

Output

[Apple, Mango, Pear]

[Apple, Mango]

 

  1. Queue Interface

 

The Java Queue interface provides a way to store and access elements in a First-In-First-Out (FIFO) manner. It is a powerful tool for managing ordered collections of data in Java and can be used to solve a variety of programming problems.The Queue interface is implemented by a number of classes, including PriorityQueue and ArrayDeque.

 

Example: Implementing the PriorityQueue class

import java.util.Queue;
import java.util.PriorityQueue;
 
public class QueueCollectionDemo {
	
	public static void main(String[] args) {
	
		// Create a new PriorityQueue
		Queue<Integer> pq = new PriorityQueue<>();
		 
		// Adding elements using add()
		pq.add(50);
		pq.add(20);
		pq.add(30);
		pq.add(40);
		
		//displaying the PriorityQueue
		System.out.println("PriorQueue "+pq);
		
		//displaying the size
		System.out.println("Number of elements after addition: "+pq.size());
		
		// Printing the top element of the PriorityQueue
		System.out.println("Accessing the top element: " +pq.peek());
		
		// Printing the top element and removing it
		System.out.println("Removing the top element: " +pq.poll());
		 
		//displaying the PriorityQueue
		System.out.println("New PriorityQueue " + pq);
	}
}

Output

PriorQueue [20, 40, 30, 50]

Number of elements after addition: 4

Accessing the top element: 20

Removing the top element: 20

New PriorityQueue [30, 40, 50]

     

  1. Set Interface

 

The Java Set interface defines an unordered collection of unique elements. It is a powerful tool for storing and manipulating sets of data in Java. The Set interface is implemented by a number of classes, each with its own unique characteristics and performance characteristics.The Set interface is implemented by a number of classes, including HashSet, LinkedHashSet, and TreeSet.

 

Example: Implementing the HashSet class

import java.util.HashSet;
import java.util.Set;
 
public class SetCollectionDemo {
	
	public static void main(String[] args) {
		
		// Create a new HashSet
		Set<String> str = new HashSet<>();
		 
		//add elements
		str.add("Apple");
		str.add("Mango");
		str.add("Pear");
		str.add("Banana");
		
		//displaying the HashSet
		System.out.println(str);
		
		//displaying the size
		System.out.println("Number of elements after addition: "+str.size());
		
		//remove element using value
		str.remove("Pear");
		 
		//display the new HashSet
		System.out.println(str);
	}
}

Output

[Apple, Pear, Mango, Banana]

Number of elements after addition: 4

[Apple, Mango, Banana]

 



Thanks for feedback.



Read More....
Java Exception Handling
HashMap in Java
Java : HashSet and LinkedHashSet
Inner classes in Java
Java Data Types and Variables - Declaration and Initialization