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<>();
  
      list1.add("Apple");
      list1.add("Mango");
      list1.add("Banana");
      System.out.println("List 1 : " + list1);
  
      list2.addAll(list1);
      list2.add("Woodapple");
      System.out.println("List 2 : " + list2);
  
      list2.retainAll(list1);
      System.out.println("List 2 : " + list2);
  
      list2.remove("Apple");
      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));
  
      list1.clear();
      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<>();
  
        list.add("Apple");
        list.add("Mango");
        list.add("Banana");
        list.add("Woodapple");
  
        //Just print the list
        System.out.println(list);
  
  
        //Using a while loop
        int i = 0;
        while(i < list.size()){
            System.out.print(list.get(i) + " ");
            i++;
        }
        System.out.println();
  
  
        //Using a for loop
        for (int j=0; j < list.size(); j++) {
            System.out.print(list.get(j) + " ");
        }
        System.out.println();
  
  
        //print using foreach loop
        for(String s : list){
             System.out.print(s + " ");
        }
        System.out.println();

  
        //print using iterator
        Iterator iterator = list.iterator();
  
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();

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

    }
}



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.




Collection interface Collection interface Reviewed by Ravi Yasas on 4:49 PM Rating: 5

No comments:

Powered by Blogger.