Important Key Points about Java Collection Interface
Basic Unit of Collection Framework:
Data Manipulation:
Standardized Behavior:
Extendable:
Polymorphic Arguments:
Subinterfaces:
Stream Support:
Collection Interface Class Diagram
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: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
[]
Collection <String> fruitCollection = new ArrayList<>();
Collection Interface Important Methods with Examples
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?
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);
}