Collection Interface in Java
- The Collection interface is a foundation of the Java Collections Framework, which gives a unified architecture for storing and manipulating bunches of objects.
- It characterizes the fundamental structure for different types of collections, such as lists, sets, and queues, and serves as the root interface for all collection classes in Java.
- Understanding how the Collection interface works is basic for utilizing collections viably in Java programming.

What is the Collection Interface in Java?
- The Collection interface is part of the java.util package and represents a bunch of objects, known as components.
- It is the parent interface for all collection classes, but it does not itself execute any particular information structure.
- Instep, it characterizes the core strategies that all collection classes must execute.
- These strategies include operations to add, remove, and query components inside a collection.
- Whereas the Collection interface gives methods for dealing with person components, it does not indicate the basic structure for storing these components (such as whether it is ordered, unique, or indexed).
- This adaptability permits Java to support different types of collections with distinctive characteristics, such as List, Set, and Queue.
Key Methods of the Collection Interface in Java
The Collection interface characterizes a few fundamental methods that offer assistance manage components inside any collection. A few of the key methods are:
- add(E e): Adds the desired component to the collection. Returns true in case the collection changes as a result of this operation.
- remove(Object o): Removes a single occurrence of the desired component from the collection on the off chance that it is present.
- size(): Returns the number of components within the collection.
- isEmpty(): Returns true in case the collection contains no components.
- contains(Object o): Checks in case the collection contains the required component.
- clear(): Removes all components from the collection.
- iterator(): Returns an iterator to repeat through the components of the collection.
These methods make the Collection interface a capable tool for managing information. Be that as it may, since the interface itself is so common, distinctive collection types execute these methods in ways suited to their particular behavior.
Types of Collections Implementing the Collection Interface in Java
The Collection interface has numerous direct subinterfaces, each characterizing a particular type of collection, with its possess unique features:
1.List:
- A collection that keeps up the order of components and permits duplicate entries.
- The List interface expands Collection and is actualized by classes like ArrayList and LinkedList. It permits components to be accessed by index.
2.Set:
- A collection that does not permit duplicate components.
- The Set interface expands Collection, and classes like HashSet and TreeSet actualize it.
- Sets are regularly utilized when uniqueness could be a need.
3.Queue:
- A collection designed for holding components earlier to handling.
- The Queue interface expands Collection and is executed by classes like LinkedList and PriorityQueue.
- Queues take after a First-In-First-Out (FIFO) order.
4.Deque:
- A subtype of Queue that permits components to be added or removed from both ends.
- Classes like ArrayDeque and LinkedList actualize the Deque interface, supporting more adaptable queue operations.
Example 1:Using the List Interface (ArrayList)
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
// Creating a List using ArrayList
Collection<String> list = new ArrayList<>();
// Adding elements to the list
list.add("Apple");
list.add("Banana");
list.add("Cherry");
// Displaying the size of the list
System.out.println("Size of the list: " + list.size()); // Output: 3
// Checking if an element is in the list
System.out.println("Contains 'Banana'? " + list.contains("Banana")); // Output: true
// Removing an element from the list
list.remove("Banana");
System.out.println("Contains 'Banana'? " + list.contains("Banana")); // Output: false
// Iterating through the list
for (String fruit : list) {
System.out.println(fruit);
}
}
}
In this example, we make an ArrayList, which could be a List usage of the Collection interface. We utilize methods like add(), contains(), and remove() to control the list, and after that emphasize through the list to print its components.
Example 2: Using the Set Interface (HashSet)
import java.util.*;
public class SetExample {
public static void main(String[] args) {
// Creating a Set using HashSet
Collection<String> set = new HashSet<>();
// Adding elements to the set
set.add("Dog");
set.add("Cat");
set.add("Rabbit");
set.add("Cat"); // Duplicate element
// Displaying the size of the set
System.out.println("Size of the set: " + set.size()); // Output: 3 (Duplicates are not allowed)
// Checking if an element is in the set
System.out.println("Contains 'Rabbit'? " + set.contains("Rabbit")); // Output: true
// Removing an element from the set
set.remove("Cat");
System.out.println("Contains 'Cat'? " + set.contains("Cat")); // Output: false
// Iterating through the set
for (String animal : set) {
System.out.println(animal);
}
}
}
Here, we make a HashSet, a Set usage, and illustrate how it consequently removes duplicates. Indeed in spite of the fact that we attempted to include “Cat” twice, it as it were shows up once within the set.
Example 3: Using the Queue Interface (LinkedList)
import java.util.*;
public class QueueExample {
public static void main(String[] args) {
// Creating a Queue using LinkedList
Collection<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.add("John");
queue.add("Alice");
queue.add("Bob");
// Displaying the size of the queue
System.out.println("Size of the queue: " + queue.size()); // Output: 3
// Removing the front element from the queue
System.out.println("Removed: " + ((LinkedList<String>) queue).poll()); // Output: John
// Iterating through the queue
for (String person : queue) {
System.out.println(person);
}
}
}
In this example, we utilize a LinkedList to actualize the Queue interface, performing operations like including components and removing from the front of the queue utilizing the poll() method.