Java Collection API - Java Tutorials

Java Collection API

Collection was added to Java with J2SE 1.2 release. Collection framework is
provided in 'java.util.package'.

All collections framework contains the following three parts :

Interfaces : Interfaces allow collections to be manipulated
independently of the details of their representation.

Implementations i.e. Classes: These are the concrete implementations
of the collection interfaces. Also, they are reusable data structures.

Algorithms: These are the methods that perform useful computations,
such as searching and sorting, on objects that implement collection interfaces.
The same method can be used on many different implementations of the appropriate
collection interface.

2. It allows different types of collections to work in a similar manner and
with a high degree of interoperability.

3. It allows the integration of standard arrays into the Collection
Framework.

Collection Interface

The Collection Interface is the base on which the Collection framework is
built. The methods declare by this Interface can be used by any collection. The
UnsupportedOperationException can be thrown by many of these methods.
These methods are summarized below :

Methods

Description

boolean add(Object obj)

Adds obj to the invoking collection. Returns true if obj
was added to the collection. Returns false if obj is already
a member of the collection, or if the collection does not
allow duplicates.

boolean addAll(Collection c)

Adds all the elements of c to the invoking collection. Returns
true if the operation succeeded (i.e., the elements were
added). Otherwise, returns false.

void clear( )

Removes all elements from the invoking collection.

boolean contains(Object obj)

Returns true if obj is an element of the invoking collection.
Otherwise, returns false.