Java Collection API - Java Tutorials


Java Collection API - Java Tutorials

Java Collection API

Java Collection API

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.

The main benefits of Collection Framework are :

1. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.

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.
boolean containsAll(Collection c)  Returns true if the invoking collection contains all elements
 of c. Otherwise, returns false.
boolean equals(Object obj)  Returns true if the invoking collection and obj are equal.
Otherwise, returns false.
int hashCode( )  Returns the hash code for the invoking collection.
boolean isEmpty( )  Returns true if the invoking collection is empty. Otherwise,
returns false.
Iterator iterator( )  Returns an iterator for the invoking collection.
boolean remove(Object obj)  Removes one instance of obj from the invoking collection.
Returns true if the element was removed. Otherwise, returns
boolean removeAll(Collection c)  Removes all elements of c from the invoking collection.
Returns true if the collection changed (i.e., elements were
removed). Otherwise, returns false.
boolean retainAll(Collection c) Removes all elements from the invoking collection except
those in c. Returns true if the collection changed
int size( )  Returns the number of elements held in the invoking collection.
Object[ ] toArray( ) Returns an array that contains all the elements stored in the
invoking collection. The array elements are copies of the
 collection elements.
Object[ ] toArray(Object array[ ]) Returns an array containing only those collection elements
whose type matches that of array.

Example :

In the below example, the method "add" of Collection Interface is implemented by various classes :

import java.util.*;

public class CollectionInterfaceDemo {
 public static void main(String[] args) {
	List AL = new ArrayList();
	System.out.println("Elements of ArrayList");
	System.out.print("\t" + AL);

	List LL = new LinkedList();
	System.out.println("Elements of LinkedList ");
	System.out.print("\t" + LL);

	Set HS = new HashSet();
	System.out.println("Elements of set");
	System.out.print("\t" + HS);

	Map HM = new HashMap();
	HM.put("ANKIT", "8");
	HM.put("MAHIMA", "24");
	HM.put("DIVYA", "31");
	HM.put("MANISHA", "12");
	HM.put("VINITA", "14");
	System.out.println("Elements of Map");
	System.out.print("\t" + HM);


Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac

C:\Program Files\Java\jdk1.6.0_18\bin>java CollectionInterfaceDemo
Elements of ArrayList
Elements of LinkedList
Elements of set
Elements of Map