Collection Framework In JAVA

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

The entire collections framework is designed around a set of standard interfaces. Several standard implementations such as LinkedList, HashSet, and TreeSet, of these interfaces are provided that you may use as it is and you may also implement your own collection.

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following −

  • Interfaces − These are abstract data types that represent collections. The Collection interface is the root interface of the collections framework hierarchy.Interfaces allow collections to be manipulated independently of the details of their representation.
  • Classes − These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
  • Algorithms − These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces.

Java does not provide direct implementations of the Collection interface but provides implementations of its subInterfaces like List, Set, and Queue. All the methods of the Collection interface are also present in its subinterfaces.

Here are the subinterfaces of the Collection Interface:

List Interface

The List interface is an ordered collection that allows us to add and remove elements like an array.Since List is an interface, we cannot create objects from it. In order to use functionalities of the List interface, we can use below given classes:

These classes are defined in the Collections framework and implement the List interface.

// ArrayList implementation of List
List<String> list1 = new ArrayList<>();

// LinkedList implementation of List
List<String> list2 = new LinkedList<>();
//Vector implementation of List
List<String> list3 = new Vector<>();

Methods of List

The List interface includes all the methods of the Collection interface. Its because Collection is a super interface of List.

Some of the commonly used methods of the Collection interface that's also available in the List interface are:

  • add() - adds an element to a list
  • addAll() - adds all elements of one list to another
  • get() - helps to randomly access elements from lists
  • iterator() - returns iterator object that can be used to sequentially access elements of lists
  • set() - changes elements of lists
  • remove() - removes an element from the list
  • removeAll() - removes all the elements from the list
  • size() - returns the length of lists

Implementation of ArrayList:

import java.util.List;
import java.util.ArrayList;

class Main {

public static void main(String[] args) {
// Creating list using the ArrayList class
List<Integer> numbers = new ArrayList<>();

// Add elements to the list
numbers.add(1);
numbers.add(2);
numbers.add(3);
System.out.println("List: " + numbers);

// Access element from the list
int number = numbers.get(2);
System.out.println("Accessed Element: " + number);

// Remove element from the list
int removedNumber = numbers.remove(1);
System.out.println("Removed Element: " + removedNumber);
}
}

Output:-

List: [1, 2, 3]
Accessed Element: 3
Removed Element: 2

Set Interface

The Set interface allows us to store elements in different sets similar to the set in mathematics. It cannot have duplicate elements.

Since Set is an interface, we cannot create objects from it. Below mentioned classes

// Set implementation using HashSet
Set<String> animals = new HashSet<>();

Here, we have created a Set called animals. We have used the HashSet class to implement the Set interface.

Methods of Set

The Set interface includes all the methods of the Collection interface. It's because Collection is a super interface of Set.

Some of the commonly used methods of the Collection interface that's also available in the Set interface are:

  • add()— adds the specified element to the set
  • addAll() — adds all the elements of the specified collection to the set
  • iterator() — returns an iterator that can be used to access elements of the set sequentially
  • remove()— removes the specified element from the set
  • removeAll() — removes all the elements from the set that is present in another specified set
  • size() — returns the length (number of elements) of the set
  • contains()— returns true if the set contains the specified element
  • hashCode() — returns a hash code value (address of the element in the set)

Implementation of HashSet Class:

import java.util.Set;
import java.util.HashSet;

class Main {

public static void main(String[] args) {
// Creating a set using the HashSet class
Set<Integer> set1 = new HashSet<>();

// Add elements to the set1
set1.add(2);
set1.add(3);
System.out.println("Set1: " + set1);

// Creating another set using the HashSet class
Set<Integer> set2 = new HashSet<>();

// Add elements
set2.add(1);
set2.add(2);
System.out.println("Set2: " + set2);

// Union of two sets
set2.addAll(set1);
System.out.println("Union is: " + set2);
}
}

Output:-

Set1: [2, 3]
Set2: [1, 2]
Union is: [1, 2, 3]

Queue Interface

The Queue interface is used when we want to store and access elements in First In, First Out manner.

Queue is an interface, In order to use the functionalities of Queue, we need to use classes that implement it:

/ LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();

// Array implementation of Queue
Queue<String> animal2 = new ArrayDeque<>();

// Priority Queue implementation of Queue
Queue<String> animal 3 = new PriorityQueue<>();

Methods of Queue

The Queue interface includes all the methods of the Collection interface. It is because Collection is the super interface of Queue.

Some of the commonly used methods of the Queue interface are:

  • add()— Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.
  • offer() — Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.
  • element() — Returns the head of the queue. Throws an exception if the queue is empty.
  • peek() — Returns the head of the queue. Returns null if the queue is empty.
  • remove() — Returns and removes the head of the queue. Throws an exception if the queue is empty.
  • poll()— Returns and removes the head of the queue. Returns null if the queue is empty.

Implementation of Queue Class

import java.util.Queue;
import java.util.LinkedList;

class Main {

public static void main(String[] args) {
// Creating Queue using the LinkedList class
Queue<Integer> numbers = new LinkedList<>();

// offer elements to the Queue
numbers.offer(1);
numbers.offer(2);
numbers.offer(3);
System.out.println("Queue: " + numbers);

// Access elements of the Queue
int accessedNumber = numbers.peek();
System.out.println("Accessed Element: " + accessedNumber);

// Remove elements from the Queue
int removedNumber = numbers.poll();
System.out.println("Removed Element: " + removedNumber);

System.out.println("Updated Queue: " + numbers);
}
}

Output:-

Queue: [1, 2, 3]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 3]

Java Map Interface

In Java, the Map interface allows elements to be stored in key/value pairs. Keys are unique names that can be used to access a particular element in a map. And, each key has a single value associated with it.

classes which implements maps are:-

/ Map implementation using HashMap
Map<Key, Value> numbers = new HashMap<>();

Methods of Map

The Map interface includes all the methods of the Collection interface. It is because Collection is a super interface of Map.

Besides methods available in the Collection interface, the Map interface also includes the following methods:

  • put(K,V) — Inserts the association of a key K and a value V into the map. If the key is already present, the new value replaces the old value.
  • putAll() — Inserts all the entries from the specified map to this map.
  • get(K) — Returns the value associated with the specified key K. If the key is not found, it returns null.
  • containsKey() — Checks if the specified key K is present in the map or not.
  • containsValue() — Checks if the specified value V is present in the map or not.
  • replace(K,V) — Replace the value of the key K with the new specified value V.
  • remove(K) — Removes the entry from the map represented by the key K.
  • remove(K,V) — Removes the entry from the map that has key K associated with value V.

Implementation of HashMap Class:-

import java.util.Map;
import java.util.HashMap;

class Main {

public static void main(String[] args) {
// Creating a map using the HashMap
Map<String, Integer> numbers = new HashMap<>();

// Insert elements to the map
numbers.put("One", 1);
numbers.put("Two", 2);
System.out.println("Map: " + numbers);

// Access keys of the map
System.out.println("Keys: " + numbers.keySet());

// Access values of the map
System.out.println("Values: " + numbers.values());

// Access entries of the map
System.out.println("Entries: " + numbers.entrySet());

// Remove Elements from the map
int value = numbers.remove("Two");
System.out.println("Removed Value: " + value);
}
}

output:-

Map: {One=1, Two=2}
Keys: [One, Two]
Values: [1, 2]
Entries: [One=1, Two=2]
Removed Value: 2

Conclusion:-

Collection framework gives us the advantage of not having to write code to implement these data structures and algorithms manually.Our code will be much more efficient as the collections framework is highly optimized.