Tuesday, June 21, 2016

Collection interface

Collection interface is the core interface of List, Queue and Set. It consist of core operations that can be used to work with above interfaces.

Basic methods of Collection interface

You may look at the following example which is done using List interface.

package com.app.collections;

import java.util.LinkedList;
import java.util.List;

public class CollectionDemo {

   public static void main(String[] args) {
      List list1 = new LinkedList<>();
      List list2 = new LinkedList<>();
      System.out.println("List 1 : " + list1);
      System.out.println("List 2 : " + list2);
      System.out.println("List 2 : " + list2);
      System.out.println("List 2 : " + list2); 
      System.out.println("isEmpty()     : " + list2.isEmpty());
      System.out.println("size()        : " + list2.size());
      System.out.println("hashCode()    : " + list2.hashCode());
      System.out.println("equals()      : " + list2.equals("Apple"));
      System.out.println("contains()    : " + list2.contains("Apple"));
      System.out.println("containsAll() : " + list1.containsAll(list2));
      System.out.println("clear() : " + list1);


I think now you have a basic idea of using Collection interface.

Traverse on a collection

There are three main ways to traverse on a collection.
  1. Using loops
  2. Using iterators
  3. Using streams
Third concept is a new one which is introduced after Java 8. At the moment I skip it and looking through first and second ways. Look at the following example.

package com.app.collections;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class CollectionTraverse {

    public static void main(String[] args) {
        List list = new LinkedList<>();
        //Just print the list
        //Using a while loop
        int i = 0;
        while(i < list.size()){
            System.out.print(list.get(i) + " ");
        //Using a for loop
        for (int j=0; j < list.size(); j++) {
            System.out.print(list.get(j) + " ");
        //print using foreach loop
        for(String s : list){
             System.out.print(s + " ");

        //print using iterator
        Iterator iterator = list.iterator();
            System.out.print(iterator.next() + " ");

        //print using Java 8 foreach loop
        list.forEach(name -> System.out.print(name + " "));


Serializable interface

This is another important thing when you think about Collection interface. I think you may heard about serialization. Serialization is the process of convert the object into byte stream. Why we need to do this ?

  • To stream through a communication link (One JVM to another)
  • Save data in persistence storage
This is very important for collection framework. Each and every collection class implements Serializable interface by default.

Cloneable interface

This is another important interface which each and every Collection classes are implemented. Cloneable interface is used to get a exact clone of objects for future needs. 

Just think once you got a list of objects, you want to manage it. While you are doing this, sometimes objects may be lost. At the time you'll have to get that list of objects again from the source. But you don't need to worry about this, because every Collection class is implemented Cloneable interface and it can be easily get the same set of objects again.