Java Collection Interface



Java Collection Interface :

  1. Collection represents a group of objects known as its elements.
  2. Collection interface is main base of the collections framework concept.
  3. Collection interface is implemented by all the collection classes , thereby inheriting the abstract methods of the interface.
  4. Following interfaces (collection types) extends the Collection interface:
    • List
    • Set
    • SortedSet
    • NavigableSet
    • Queue
    • Deque

Collection Methods and Description :

No Methods with Description
1
boolean add(Object obj)

Purpose : Adds obj to the 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.

2
boolean addAll(Collection c1)

Purpose : Adds all the elements of c1 to the invoking collection.
Returns True : on success
Returns False : on failure

3
void clear( )

Purpose : Removes all elements from the invoking collection.
Returns True :
Returns False :

4
boolean contains(Object obj)

Purpose : Check whether element is present in list or not.
Returns True : if obj is an element of the invoking collection
Returns False : if obj is not an element of the invoking collection

5
boolean containsAll(Collection c1)

Purpose : Check whether all elements is present in list or not.
Returns True : if the invoking collection contains all elements of c1
Returns False : if the invoking collection does not contains all elements of c1

6
boolean equals(Object obj)

Purpose : Check equality.
Returns True : if the invoking collection and obj are equal
Returns False : if the invoking collection and obj are not equal

7
int hashCode( )

Purpose : Returns the hash code for the invoking collection

8
boolean isEmpty( )

Returns true if the invoking collection is empty. Otherwise, returns false.

9
Iterator iterator( )

Returns an iterator for the invoking collection.

10
boolean remove(Object obj)

Removes one instance of obj from the invoking collection. Returns true if the element was removed. Otherwise, returns false.

11
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.

12
boolean retainAll(Collection c)

Removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false

13
int size( )

Returns the number of elements held in the invoking collection.

14
Object[ ] toArray( )

Returns an array that contains all the elements stored in the invoking collection. The array elements are copies of the collection elements.

15
Object[ ] toArray(Object array[ ])

Returns an array containing only those collection elements whose type matches that of array.

Example : Collection Interface

package com.c4learn.collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CollectionsDemo {

   public static void main(String[] args) {
	   
      List a1 = new ArrayList();
      a1.add("Pritesh");
      a1.add("Suraj");
      a1.add("Aman");
      System.out.println("ArrayList  : " + a1);

      List l1 = new LinkedList();
      l1.add("Pritesh");
      l1.add("Suraj");
      l1.add("Aman");
      System.out.println("LinkedList : " + l1);

      Set s1 = new HashSet(); 
      s1.add("Pritesh");
      s1.add("Suraj");
      s1.add("Aman");
      System.out.println("Set        : " + l1);

      Map m1 = new HashMap(); 
      m1.put("Pritesh", "8");
      m1.put("Suraj", "31");
      m1.put("Aman", "12");
      m1.put("Sachin", "14");
      System.out.println("Map        : " + m1);
   }
}

Output :

ArrayList  : [Pritesh, Suraj, Aman]
LinkedList : [Pritesh, Suraj, Aman]
Set        : [Pritesh, Suraj, Aman]
Map        : {Suraj=31, Aman=12, Pritesh=8, Sachin=14}