Wednesday, June 22, 2016

List interface



In my first post of collection you may have an idea about, how to choose a collection. In this post I'm gonna talk about List interface.

List interface can be used if you need an ordered collection which can also have duplicates. There are four special classes which implement List interface.
  1. java.util.ArrayList
  2. java.util.LinkedList
  3. java.util.Vector
  4. java.util.Stack

ArrayList

  • Implemented by using a re-sizable array (Dynamic array).
  • It can contain duplicates.
  • ArrayList is not synchronized .
  • Maintain insertion order.
  • Default initial capacity is very small. Once it is filled its size will be incremented by 50%.
  • It allows random access and it is fast. But Inserting and deleting is slow.
  • This uses iterator to traverse.

LinkedList

  • Implemented by using doubly LinkedList
  • Duplicates are allowed.
  • LinkedList is not synchronized.
  • Maintain insertion order.
  • LinkedList allows sequential access and it slow, but inserting and deleting is fast.
  • LinkedList doesn't have initial size.
  • Compare to ArrayList and Vector, LinkedLIst uses more memory.

Vector

  • Vector looks same as ArrayList.
  • Main difference is, Vector is synchronized.
  • Accessibility is slow, because Vector is synchronized.
  • Once it is filled, initial capacity is incremented by 100%.
  • Vector uses Enumeration to traverse.

Stack

  • This is another simple class which is extended to Vector class.
  • Stack is Last In First Out (LIFO) manner collection.
  • Stack is also synchronized just like Vector, then Stack is also slow.
  • Stack has few special methods, push(), pop() and peek().
Look at the following example about simple operations of Stack.

import java.util.Stack;

public class StackDemo {

   public static void main(String[] args) {
  
       Stack<Integer> stack = new Stack<>();
       stack.push(10);
       stack.push(30);
       stack.push(20);
       stack.push(50);
  
       System.out.println(stack);
       stack.pop();
       System.out.println("After pop : " + stack);
       System.out.println("Head of stack : " + stack.peek());
    }
}




Vector and Stack was introduced in 1.0 version while ArrayList, LinkedList and Collection was introduced in 1.2 version of Java. Because of that, Vector and Stack is called "legacy classes".


You may look at the following example to get an idea about List interface. This example is done by using ArrayList and it can be implemented by using LinkedList and also using a Vector.


public class ArrayListDemo {
 
   public static void main(String[] args) {
  
      List<Integer> list = new ArrayList<>();
      List<Integer> list2 = new ArrayList<>();
  
      list.add(10); //add element to the list
      list.add(20);
      list.add(30);
      list.add(30);
      list.add(0, 100); //add element to given index
      System.out.println(list);
  
      list.remove(1); //remove the element from given index
      System.out.println(list.isEmpty()); //check the list is empty or not
      System.out.println(list.size()); //get the size of list
      System.out.println(list.contains(30)); //check given value is there in list
      System.out.println(list.indexOf(30)); //give index of given value
      System.out.println(list.get(2)); //get value of given index
      System.out.println(list.equals(list2)); //compare two lists
      System.out.println(list.lastIndexOf(100)); //return last index of given value
      System.out.println(list.lastIndexOf(70)); //if not return -1
      System.out.println(list);
  
      list.set(0, 200); //set value to given index
      System.out.println(list);
  
      list.clear(); //clear list
      System.out.println(list);
   } 
}


Using Iterator to traverse

  • Iterator can be used to traverse on ArrayList and LinkedList.
  • You can also use loops to traverse.
  • Look at the following example.

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

public class ListSpecialMethods {

   public static void main(String[] args) {
  
       List<Integer> linkedList = new LinkedList<>();
       linkedList.add(10);
       linkedList.add(20);
       linkedList.add(30);
       linkedList.add(10);
       linkedList.add(60);
       linkedList.add(80);
  
       showData(linkedList);
   }
  
 
   public static <E> void showData(List<E> list){
       Iterator<E> iterator = list.iterator();
  
       while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
       }
   }
}

ArrayList vs LinkedList vs Vector vs Stack



RandomAccess interface

  • This is very important when talking about ArrayList and Vector classes.
  • These two classes implements RandomAccess interface.
  • RandomAccess interface is a Marker interface like Serializable and Cloneable interfaces. There is no methods present in these marker interfaces.
  • These interfaces identify classes with the capability of they are defined.

Now you know both ArrayList and Vector classes implement RandomAccess interface. So when you searching and accessing any data inside of ArrayList or Vector, it is too fast than others.