www.000webhost.com
/** * Created by PhpStorm. * User: Rahul * Date: 12/8/2016 * Time: 8:09 PM */ Collection interface


Collection Interface


Core Java

This is the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Java platform doesn’t provide any direct implementations of this interface.


There might be confusion between Java collection and Collections

Collection is a base interface for most collection classes, where as Collections is a utility class.


Methods available in collection with method Discription's

	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(i.e., elements were removed). Otherwise, returns false

	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.
			

Source Code Example of Important/Usefull Methods available in collection


Example of add() and addAll() in collection

	
package com.javaessential;

import java.util.*;

public class CollectionsAddMethodDemo {
   public static void main(String args[]) {
      // create array list object       
      List arrlist = new ArrayList();
      
      // populate the list
      arrlist.add("A");
      arrlist.add("B");
      arrlist.add("C");  
      
      System.out.println("Initial collection value: "+arrlist);
      
      // add values to this collection
      boolean b = Collections.addAll(arrlist, "1","2","3");
      
      System.out.println("Final collection value: "+arrlist);
   }    
}

compile and run the above program, this will produce the following result.

	Initial collection value: [A, B, C]
	Final collection value: [A, B, C, 1, 2, 3]

Source code example of contains() and containsAll()


package com.javaessential;

import java.util.ArrayList;

public class ContainsMethodDemo {
   public static void main(String[] args) {
      
      // create an empty array list with an initial capacity
      ArrayList arrlist = new ArrayList();

      // use add() method to add elements in the list
      arrlist.add(20);
      arrlist.add(25);
      arrlist.add(10);
      arrlist.add(15);        
       
      // let us print all the elements available in list
      for (Integer number : arrlist) {
         System.out.println("Number = " + number);
       }

      // list contains element 10
      boolean retval = arrlist.contains(10); 
	  
      if (retval == true) {
        System.out.println("element 10 is contained in the list");
       }
      else {
        System.out.println("element 10 is not contained in the list");
       }
		
      // list does not contain element 30
      boolean retval2 = arrlist.contains(30);
	  
      if (retval2 == true) {
        System.out.println("element 30 is contained in the list");
       }
      else {
        System.out.println("element 30 is not contained in the list");    
       }
   }
}

Compile the above program and output is

	Number = 20
	Number = 25
	Number = 10
	Number = 15
	element 10 is contained in the list
	element 30 is not contained in the list

//Breif Description with dummy code
Collection collection   = new HashSet();
boolean containsElement = collection.contains("an element");
 
Collection elements     = new HashSet();
boolean containsAll     = collection.containsAll(elements);

//Description 
contains() returns true, when elements are present in collection, & false if not. 
containsAll() returns true when contains all the elements are present in the parameter collection & false if not.

Source code example of equals() method

public class JavaessentialEqualsDemo 
{
  public static void main(String args[]) 
  {
      String Str1 = new String("This is not immutable!!");
      String Str2 = Str1;
      String Str3 = new String("This is not immutable!!");
      boolean compare1,compare2;

      compare1 = Str1.equals( Str2 );
      System.out.println("Returned Value = " + compare1 );

      compare2 = Str1.equals( Str3 );
      System.out.println("Returned Value = " + compare2 );
   }
}

//Output : 
Returned Value = true
Returned Value = true

Example with source code of clear() method


package com.javaessential.collection;

import java.util.Vector;

public class ClearVectorExample {
  public static void main(String[] args) {
    // Create a Vector and populate it with elements

    Vector vector = new Vector();
    vector.add("element_1");
    vector.add("element_2");
    vector.add("element_3");
    
    System.out.println("Vector size before removing elements : " + vector.size());

    // Vector clear() operation removes all elements
    vector.clear();
    System.out.println("Vector size after removing elements : " + vector.size());
  }
}
//Output : - 
Vector size before removing elements : 3
Vector size after removing elements : 0

remove() removeAll() Example with source code

package javaessential.com;

import java.util.List;
import java.util.ArrayList;

public class ListRemoveAll {
	public static void main(String args[]){
		ArrayList al =new ArrayList();
		ArrayList al2 =new ArrayList();
		al.add(1);
		al.add(2);
		al.add(3);
		al.add(4);
		al.add(5);
		System.out.println("Elements of first list are : "+al);
		System.out.println("Size of first list = " +al.size());
		al2.add("Suman");
		al2.add("Raman");
		al2.add("Ranjan");
		al2.add("Niranjan");
		System.out.println("Elements of second list are : "+al2);
		System.out.println("Size of second list = " +al2.size());
		boolean b=al.addAll(al2);
		System.out.println("After addition of list1 and list2 new list = " );
		System.out.println("\t"+al);
		System.out.println("Size of list after addition of two lists = " +al.size());
		
		//remove() is also work like removeAll() remove() worked on an Element
		//while removeAll() work on some set of element
		boolean bol=al.removeAll(al2);
		System.out.println("List2 is removed : " +bol);
		System.out.println("After removing elements of list2 from new list then elements of list1 = ");
		System.out.println("\t\t"+al);
		System.out.println("Size of list after removing the list2 = " +al.size());
	}
}

//Output : - 

			lements of first list are : [1, 2, 3, 4, 5]
			
			Size of first list = 5
			
			Elements of second list are : [Suman, Raman, Ranjan, Niranjan]
			
			Size of second list = 4
			
			After addition of list1 and list2 new list =
			
			[1, 2, 3, 4, 5, Suman, Raman, Ranjan, Niranjan]
			
			Size of list after addition of two lists = 9
			
			List2 is removed : true
			
			After removing elements of list2 from new list then elements of list1 =
			
			[1, 2, 3, 4, 5]
			
			Size of list after removing the list2 = 5
			
--> Click here to continue reading - List Interface