Collections Framework – The Collection Interface

In this chapter, we will learn about the Collection interface of the Java Collections framework.

Here are the key points about Collection interface:
  • If we want to represent a group of individual objects as a single entity then we should go for collections.
  • Collection Interface is considered as the root interface of the Collection Framework.
  • Collection Interface defines the most common methods which are applicable for any collection object.
  • List, Queue, and Set are all subinterfaces of Collection interface. 
  • JDK does not provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub-interfaces.

Collection Interface Class Diagram

Here is the class diagram of the Collection interface.
From the above class diagram, the Collection interface extends the Iterable interface which is a member of the java.lang package.  Implementing the Iterable interface allows an object to be the target of the "for-each loop" statement.

Collection Interface Methods/APIs

  • boolean add(E e) - Ensures that this collection contains the specified element (optional operation).
  • boolean addAll(Collection<? extends E> c) - Adds all of the elements in the specified collection to this collection (optional operation).
  • void clear()- Removes all of the elements from this collection (optional operation).
  • boolean contains(Object o) - Returns true if this collection contains the specified element.
  • boolean containsAll(Collection<?> c) - Returns true if this collection contains all of the elements in the specified collection.
  • boolean equals(Object o)- Compares the specified object with this collection for equality.
  • int hashCode() - Returns the hash code value for this collection.
  • boolean isEmpty() - Returns true if this collection contains no elements.
  • Iterator iterator() - Returns an iterator over the elements in this collection.
  • default Stream parallelStream() - Returns a possibly parallel Stream with this collection as its source.
  • boolean remove(Object o)-  Removes a single instance of the specified element from this collection, if it is present (optional operation).
  • boolean removeAll(Collection<?> c) - Removes all of this collection's elements that are also contained in the specified collection (optional operation).
  • default boolean removeIf(Predicate<? super E> filter) - Removes all of the elements of this collection that satisfy the given predicate.
  • boolean retainAll(Collection<?> c) - Retains only the elements in this collection that are contained in the specified collection (optional operation).
  • int size() - Returns the number of elements in this collection.
  • default Spliterator spliterator() - Creates a Spliterator over the elements in this collection.
  • default Stream stream() - Returns a sequential Stream with this collection as its source.
  • Object[] toArray() - Returns an array containing all of the elements in this collection.
  • T[] toArray(T[] a) - Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Collection Interface Example

The following example demonstrates the usage of important Collection interface methods:
package com.java.collections.interfaces;

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 JDK 8 and later, the Collection interface also exposes methods Stream stream() and Stream parallelStream(), for obtaining sequential or parallel streams from the underlying collection. (See the lesson entitled Aggregate Operations for more information about using streams.)
This is demonstrated in the below posts:

What's next?

In this chapter, we have learned all about the Collection interface, it's methods, and usage of a few common methods with examples.

Comments

  1. syntax error in CollectionDemo class as "element cannot be resolved to a variable"
    Please check once.

    ReplyDelete
  2. this can be used in place of for each :-

    for (String fruit : fruitCollection) {
    System.out.println(fruit);
    }

    ReplyDelete

Post a comment