This section contains the detail about the Collection Interface in java.
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 false. |
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(); AL.add("ANKIT"); AL.add("DIVYA"); AL.add("MANISHA"); System.out.println("Elements of ArrayList"); System.out.print("\t" + AL); List LL = new LinkedList(); LL.add("ANKIT"); LL.add("DIVYA"); LL.add("MANISHA"); System.out.println(); System.out.println("Elements of LinkedList "); System.out.print("\t" + LL); Set HS = new HashSet(); HS.add("ANKIT"); HS.add("DIVYA"); HS.add("MANISHA"); System.out.println(); 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(); System.out.println("Elements of Map"); System.out.print("\t" + HM); } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac CollectionInterfaceDemo.java C:\Program Files\Java\jdk1.6.0_18\bin>java CollectionInterfaceDemo Elements of ArrayList [ANKIT, DIVYA, MANISHA] Elements of LinkedList [ANKIT, DIVYA, MANISHA] Elements of set [MANISHA, DIVYA, ANKIT] Elements of Map {MANISHA=12, DIVYA=31, VINITA=14, MAHIMA=24, ANKIT=8} |
[ 0 ] Comments