SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Java Collection Interface

  • Collection is an interface in java.
  • Collection interface extends Iterable interface.
  • In hierarchy, Collection interface is root of List, Queue and Set interfaces.

Java Collection Interface

  • It represents unit of its elements. i.e. group of objects.
  • Some collections do not allow duplicate elements but some allows duplicate elements.
  • Collection interface is base on which collection framework is built.
  • Collection interface contains basic operation methods as bellow.
  • boolean add(E element) - To make sure that this collection contains the specified element. It will return true if object is added to collection. Return false if object is already there and collection does not allow duplicates.
  • boolean contains(Object element) - It will return true if collection already have specified element.
  • boolean isEmpty() - Return true if collection is empty.
  • Iterator<E> iterator() - It will return an iterator for the invoking collection.
  • boolean remove(Object element) - It will remove single instance of given element from collection. Return true if it is present else it will return false.
  • int size() - It will return number of elements present in invoking collection.
  •  Collection interface also contains methods that operate on entire collections as bellow.
  • boolean addAll(Collection<? extends E> c) - It will add all elements of c to the invoking collection. Return true if operation was success else it will return false.
  • boolean containsAll(Collection<?> c) - It will return true if this collection contains all the elements of specified collection.
  • void clear() - It will remove all the elements from invoking collection.
  • boolean removeAll(Collection<?> c) - It will remove all those elements from invoking collection which are available in c. It will return true if given elements removed else it will return false.
  • boolean retainAll(Collection<?> c) - Retains all the elements in invoking collection which are available in c.
  • Also there are methods to perform array operations as bellow.
  • Object[] toArray() - Return array of elements from invoking collection.
  • <T> T[] toArray(T[] a) - Return array of all those elements from collection.
Basic Example of Collection
package JAVAExamples;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

public class CollectionExample {

 public static void main(String[] args) {

  // Collection ArrayList.
  Collection<String> c1 = new ArrayList<String>();
  // Add items in ArrayList.
  c1.add("ArrayList Item 1");
  c1.add("ArrayList Item 2");
  c1.add("ArrayList Item 3");
  c1.add("ArrayList Item 4");
  // Get collection Items list.
  System.out.println("Collection Items are : " + c1);
  // Get size of collection.
  System.out.println("Size of collection is : " + c1.size());
  // Remove item from collection.
  c1.remove("ArrayList Item 3");
  System.out.println("Collection Items are : " + c1);

  System.out.println();
  System.out.println();  
  
  
  // Collection LinkedList.
  Collection<String> c2 = new LinkedList<String>();
  // Add items in LinkedList.
  c2.add("LinkedList Item 1");
  c2.add("LinkedList Item 2");
  c2.add("LinkedList Item 3");
  c2.add("LinkedList Item 4");
  // Get collection Items list.
  System.out.println("Collection Items are : " + c2);
  // Get size of collection.
  System.out.println("Size of collection is : " + c2.size());
  // Remove item from collection.
  c2.clear();
  System.out.println("Collection Items are : " + c2);
 }
}

Output :
Collection Items are : [ArrayList Item 1, ArrayList Item 2, ArrayList Item 3, ArrayList Item 4]
Size of collection is : 4
Collection Items are : [ArrayList Item 1, ArrayList Item 2, ArrayList Item 4]


Collection Items are : [LinkedList Item 1, LinkedList Item 2, LinkedList Item 3, LinkedList Item 4]
Size of collection is : 4
Collection Items are : []


So this is basic idea of root collection interface. We will learn different child interfaces of collection interface one by one to get idea about how collection interface works.

1 comment:

  1. Dear Aravind,

    I need your help regarding selenium webdriver issues faced by me so i can we contact on skype. you can contact me on my skype account "sumitbaindi".

    ReplyDelete