Java Collection Interface

In this tutorial, we will learn about the java.util.Collection interface, guide lines to consider while implementing it and a custom example with explanation.

java.util.Collection Interface

java.util.Collection interface is top most in the hierarchy of collections, all the collection classes should implement interface or its extending-interfaces. Although there is no class with direct implementation of this interface but it sub-interfaces are implemented by the collection classes.

Before implementing collection interface, below guide lines should be considered.

Guide Lines:

  1. At most priority to check in-built classes will not be sufficient to suffice requirement.
  2. Understand each method of java.util.Collection interface and usage before implementing.
  3. Choose appropriate data structure based on the requirement and performance factors.
  4. Now implement all the methods, make sure test cases should cover all these methods.

Collection Interface methods

Methods of java.util.Collection interface:

MethodDescription
boolean add(T e)This method inserts element into the collections and returns true its success otherwise false.
boolean addAll(Collection<? extends T> c)This method inserts all the elements of given collections, returns true in case current collections is changed due to adding elements.
clear()This method removes all the elements from the collection.
boolean contains(Object o)This method returns true if current collection contains the given element otherwise false.
boolean containsAll(Collection<?> T)This method returns true if current collection contains all the elements of given collection otherwise false.
boolean equals(Object o)This method returns true ,if the specified object is equal with this collection otherwise false.
boolean isEmpty()This method returns true ,if the collection is empty otherwise false.
Iterator<T> iterator()This method returns an iterator over the elements with proper sequence.
boolean remove(Object o)This method removes the specified element from this collection of first occurrence and return true, if it is not present in the collection return false.
boolean removeAll(Collection<?> T)This method removes all the elements of this collection which are also present in the specified collection, returns true if this collection is changed after the call otherwise false.
boolean removeIf(Predicate<? super E> filter)This method removes the elements of this collection which satisfy the given filter returns true, if any element is removed otherwise false.
boolean retainAll()This method keeps only elements in this collection that are present in the specified collection, returns true if this collection is changed after the call otherwise false.
int size()This method returns the total number of the elements in this collection.
Spliterator<E> spliterator()This method returns a spliterator over the elements of this collection.
Object[] toArray()This method returns an array containing all the elements of this collection with same ordered as guaranteed by this collection.
int hashCode()This method returns hash code of this collection.

Collection interface example one:

CopiedCopy Code
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
public class CollectionInterfaceExample {
	public static void main(String[] args) {
		Collection<String> arrayCollection = new ArrayList();
		Collection<String> linkCollection = new LinkedList();
		Collection<String> setCollection = new HashSet();
		arrayCollection.add("Array Based Collection");
		linkCollection.add("Link Based Collection");
		setCollection.add("Set Based Collection");
		System.out.println(arrayCollection.toString());
		System.out.println(linkCollection.toString());
		System.out.println(setCollection.toString());
	}
}

Output:

[Array Based Collection]
[Link Based Collection]
[Set Based Collection]

Explanation:

java.util.Collection interface is a root for all the collections. Hence all the classes that are part of collection framework implemented Collection interface or its sub-interfaces. In this example, we have assigned reference of array based, link based and set based collections to it. Inserted an element and printed the collections of elements.

Collection interface example two:

In this example, we will see how collection interface can be implemented with the custom class. To keep simple, we will extend the class which is already implemented the Collection interface and this allows to customize only required methods not all the methods.

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
public class MyCollection<T> extends LinkedList<T> implements Collection<T> {
	@Override
	public int size() {
		return super.size();
	}
	@Override
	public boolean isEmpty() {
		return super.isEmpty();
	}
	@Override
	public boolean contains(Object o) {
		return super.contains(o);
	}
	@Override
	public Iterator<T> iterator() {
		return super.iterator();
	}
	@Override
	public boolean add(T e) {
		return super.add(e);
	}
	@Override
	public boolean remove(Object o) {
		return super.remove(o);
	}
}

Conclusion:

In this tutorial, we have covered what is the collection interface, guide lines and importance of it along with the examples of it.