Java Collection Interface

Category: Java   Tags: Java, Java Collection, Collection Interface

The Collection interface is the root of the collection hierarchy. Collection declares the core methods that all collections will have. It determines the fundamental nature of the collection classes. Collection is a generic interface:

                          interface Collection<E>
                                

Here, E specifies the type of objects that the collection will hold. Collection extends the Iterable interface. Therefore all collections can be cycled through by use of the for- each style for loop.

Methods

boolean add(E element)
It will add element to the invoking collection. It returns true if element is added to the collection. Returns false if element already exist in the collection. Collection does not allow duplicates.
boolean remove(Object element)
Removes a single instance of the specified element from the invoking collection. Returns true if the element was removed else returns false.
int size()
Returns the number of elements in the invoking collection.
boolean isEmpty()
Returns true if the invoking collection contains no elements.
boolean contains(Object element)
Returns true if the invoking collection contains the specified element.
Iterator iterator()
Returns an iterator over the elements in the invoking collection.
Object[] toArray()
Returns an array containing all of the elements in the invoking collection. The order of element will be maintained, if it is supported by the invoking collection. Returned array is a new array so modifying data in this array has no effect on the invoking collection.
boolean containsAll(Collection<?> c)
Returns true if the invoking collection contains all of the elements in the specified collection.
boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to the invoking collection. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. Returns true if this collection changed as a result of this operation.
boolean removeAll(Collection<?> c)
Removes all of invoking collection's elements that are also contained in the specified collection. Returns true if this collection changed as a result of this operation.
void clear()
Removes all of the elements from this collection. UnsupportedOperationException exception is thrown if the this operation is not supported by the invoking collection.

Traversing Collections

You can traverse a collection in three ways:

  1. Using aggregate operations
  2. Using for-each
  3. Using iterators

Aggregate Operations

In JDK 8 and later, the Collection interface exposes methods stream() and parallelStream(), for obtaining sequential or parallel streams from the invoking collection.

The following code sequentially iterates through a collection of employees and prints out the each employee name:

                                    employeeCollection.stream()
                                    .filter(e -> e.getDep() == Department.FINANCE)
                                    .forEach(e -> System.out.println(e.getName()));
                                

If the collection is large, parallel stream is a better choice:

                                    employeeCollection.parallelStream()
                                    .filter(e -> e.getDep() == Department.FINANCE)
                                    .forEach(e -> System.out.println(e.getName()));
                                

for-each Construct

The following code uses the for-each construct to print out each element of a collection:

                                    for (Object element : myCollection)
                                        System.out.println(element);
                                

Iterators

An Iterator is an object that enables you to traverse through a collection.

                                    Iterator itr = myCollection.iterator();
                                    while(itr.hasNext()) {
                                        System.out.println(itr.next());
                                    }