Introduction
In this guide, we will learn about the JavaCollection
Interface of the Java Collections framework. The Collection interface is considered the root interface of the Collection Framework.
Important Key Points about the Java Collection Interface
Basic Unit of Collection Framework
The Collection interface is a member of the Java Collections Framework and serves as the base interface for most of the other collection interfaces.
Data Manipulation
The Collection interface provides methods for basic operations such as add, remove, contains, size, and iterator, which allow you to manipulate and traverse collections.
Standardized Behavior
All the classes that implement the Collection
interface will have a set of common methods.
Extendable
The Collection
interface extends the Iterable
interface, which means the elements of the Collection can be iterated in a sequence.
Polymorphic Arguments
If a method takes the Collection interface as an argument, it means it can accept any object of any class that implements this interface. This makes methods more flexible and scalable.
Subinterfaces
Some of the commonly used subinterfaces of the Collection interface are Set, List, and Queue. These subinterfaces provide more specialized data structures for use in a variety of applications.
Stream Support
From Java 8, the Collection
interface has methods to support streams like stream()
and parallelStream()
to return sequential or parallel streams respectively.
Collection Interface Example
The following example demonstrates the usage of important Collection
interface methods:
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo {
public static void main(String[] args) {
Collection<String> fruitCollection = new ArrayList<>();
fruitCollection.add("banana");
fruitCollection.add("apple");
fruitCollection.add("mango");
System.out.println(fruitCollection);
fruitCollection.remove("banana");
System.out.println(fruitCollection);
System.out.println(fruitCollection.contains("apple"));
fruitCollection.forEach((element) -> {
System.out.println(element);
});
fruitCollection.clear();
System.out.println(fruitCollection);
}
}
Output:
[banana, apple, mango]
[apple, mango]
true
apple
mango
[]
In the above example, we can clearly see that the Collection is an interface and we used it as a reference type:
Collection<String> fruitCollection = new ArrayList<>();
Collection Interface Important Methods with Examples
boolean add()
Adds a specific element to the collection. Returns true if the collection is modified, false otherwise.
Collection<String> collection = new ArrayList<>();
boolean isAdded = collection.add("Hello");
System.out.println(isAdded); // Output: true
boolean addAll(Collection<? extends E> c)
Adds all elements from a specified collection to the current collection. Returns true if the collection is modified, false otherwise.
Collection<String> collection1 = new ArrayList<>();
Collection<String> collection2 = new ArrayList<>();
collection2.add("Hello");
collection2.add("World");
boolean isAllAdded = collection1.addAll(collection2);
System.out.println(isAllAdded); // Output: true
void clear()
Removes all elements from the collection.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.clear();
System.out.println(collection.size()); // Output: 0
boolean contains(Object o)
Checks if the collection contains the specified element. Returns true if it does, false otherwise.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
boolean contains = collection.contains("Hello");
System.out.println(contains); // Output: true
boolean containsAll(Collection<?> c)
Checks if the collection contains all elements from a specified collection. Returns true if it does, false otherwise.
Collection<String> collection1 = new ArrayList<>();
Collection<String> collection2 = new ArrayList<>();
collection2.add("Hello");
collection2.add("World");
collection1.add("Hello");
collection1.add("World");
boolean containsAll = collection1.containsAll(collection2);
System.out.println(containsAll); // Output: true
boolean isEmpty()
Checks if the collection is empty. Returns true if it is, false otherwise.
Collection<String> collection = new ArrayList<>();
boolean isEmpty = collection.isEmpty();
System.out.println(isEmpty); // Output: true
Iterator iterator()
Returns an iterator that can be used to traverse the collection.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
Iterator<String> iterator = collection.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next()); // Output: Hello
}
default Stream parallelStream()
Returns a possibly parallel Stream with the collection as its source.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.parallelStream().forEach(System.out::println); // Output: Hello
boolean remove(Object o)
Removes a single instance of the specified element from the collection, if present. Returns true if the collection is modified, false otherwise.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
boolean isRemoved = collection.remove("Hello");
System.out.println(isRemoved); // Output: true
boolean removeAll(Collection<?> c)
Removes all elements in the collection that are also contained in the specified collection. Returns true if the collection is modified, false otherwise.
Collection<String> collection1 = new ArrayList<>();
Collection<String> collection2 = new ArrayList<>();
collection2.add("Hello");
collection1.add("Hello");
collection1.add("World");
boolean isAllRemoved = collection1.removeAll(collection2);
System.out.println(isAllRemoved); // Output: true
boolean retainAll(Collection<?> c)
Retains only the elements in the collection that are also contained in the specified collection. Returns true if the collection is modified, false otherwise.
Collection<String> collection1 = new ArrayList<>();
Collection<String> collection2 = new ArrayList<>();
collection2.add("Hello");
collection1.add("Hello");
collection1.add("World");
boolean isRetained = collection1.retainAll(collection2);
System.out.println(isRetained); // Output: true
int size()
Returns the number of elements in the collection.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
int size = collection.size();
System.out.println(size); // Output: 1
default Spliterator spliterator()
Returns a Spliterator over the elements in the collection.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
Spliterator<String> spliterator = collection.spliterator();
spliterator.forEachRemaining(System.out::println); // Output: Hello
default Stream stream()
Returns a sequential Stream with the collection as its source.
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.stream().forEach(System.out::println); // Output: Hello
What's next?
In this guide, we have learned all about the Collection interface, its methods, and the usage of a few common methods with examples.
In the next guide, we will learn about the List interface of the Java collections framework.
Related Guides
- Collections Framework - The Collection Interface
- Collections Framework - The Set Interface
- Collections Framework - The SortedSet Interface
- Collections Framework - The List Interface
- Collections Framework - The Queue Interface
- Collections Framework - The Deque Interface
- Collections Framework - The Map Interface
- Collections Framework - The SortedMap Interface
In one word "super".
ReplyDeleteShort and simple explanation.
ReplyDeletesyntax error in CollectionDemo class as "element cannot be resolved to a variable"
ReplyDeletePlease check once.
Corrected thanks for reporting.
Deletethis can be used in place of for each :-
ReplyDeletefor (String fruit : fruitCollection) {
System.out.println(fruit);
}