![]() |
VOOZH | about |
The Collection interface is the root of the Java Collections Framework, defined in the java.util package. It represents a group of individual objects as a single unit and provides basic operations for working with them.
public interface Collection<E> extends Iterable<E>
Here: E represents the type of elements stored in the collection.
Object Creation of Collection Interface
Collection<String> fruits = new ArrayList<>();
After Removal: [Apple, Mango]
In Java, we cannot create an object of an interface directly. Instead, we create an object of the ArrayList class that implements the interface and assign it to the interface reference.
The Collection interface is part of a hierarchy that extends Iterable, which means collections can be traversed.
The sub-interfaces of Collection, also called collection types, are:
Declaration:
public interface List<E> extends Collection<E>
Declaration:
public interface Set<E> extends Collection<E>
Declaration:
public interface SortedSet<E> extends Set<E>
Declaration:
public interface NavigableSet<E> extends SortedSet<E>
Declaration:
public interface Queue<E> extends Collection<E>
Declaration:
public interface Deque<E> extends Queue<E>
The Collection interface provides several operations to manipulate data efficiently. Let’s see the most common operations using the ArrayList implementation class.
We can add elements using the add(E e) method for a single element or addAll(Collection c) to add multiple elements.
After adding elements: [10, 20, 30, 40, 50]
Elements can be removed using remove(E e) or removeAll(Collection c) methods.
Initial Collection: [Apple, Banana, Mango, Orange] After removing Mango: [Apple, Banana, Orange] After removeAll(): [Orange]
Although the Collection interface doesn’t provide index-based access, its sub-interface List (implemented by ArrayList) allows retrieving elements using the get(int index) method.
Colors List: [Red, Green, Blue] First Color: Red Last Color: Blue
To iterate over a collection we used Different types of cursor
1. Iterator
[Apple, Mango]
2. ListIterator
Forward: Red Green Blue Backward: Blue Green Red
Method | Description |
|---|---|
| add(E e) | Adds an element to the collection (optional). |
addAll(Collection<? extends E> c) | Adds all elements from another collection. |
clear() | Removes all elements. |
contains(Object o) | Checks if the collection contains the specified element. Takes Object as a parameter because it uses the equals(Object obj) method to check equality. |
containsAll(Collection<?> c) | Checks if all elements exist. |
remove(Object o) | Removes the first occurrence of the specified element. Takes Object as a parameter because it uses equals(Object obj) to identify the element to remove. |
removeAll(Collection<?> c) | Removes all elements present in another collection. |
retainAll(Collection<?> c) | Retains only elements present in another collection. |
removeIf(Predicate<? super E> filter) | Removes elements satisfying a predicate. |
size() | Returns the number of elements. |
isEmpty() | Checks if the collection is empty. |
iterator() | Returns an iterator over the elements. |
stream() | Returns a sequential stream. |
parallelStream() | Returns a parallel stream. |
toArray() | Converts the collection to an array. |
equals(Object o) | Compares this collection with another object. |
hashCode() | Returns hash code of the collection. |
spliterator() | Returns a Spliterator for elements. |
Note:
equals()determines if two elements are logically equal.hashCode()provides a hash value for efficient storage and lookup (especially in sets and hash-based collections).