List Interface:
  • List allows Duplicate Elements.
  • List having index.
  • List allows n number of null values.
  • List will display Insertion order with index.
  • List having classes like :
  • Vector
  • ArrayList
  • LinkedList
Vector:
  • Vector is a legacy class.
  • Vector is synchronized.
  • Vector initial capacity is 10.
  • Vector allows n number of null values
  • Vector can be accessible by index.
  • Vector allows Duplicate Elements.
Program for Vector:

package com.instanceofjavaforus;
import java.util.Enumeration;

import java.util.List;

import java.util.Vector;



public class A{

public static void main(String[] args) {


Vector vector=new Vector();


vector.add("indhu");

vector.add("sindhu");

vector.add("swathi");

vector.addElement(null);

vector.addElement(null);
Enumeration em=vector.elements();
while(em.hasMoreElements()){
System.out.println(em.nextElement());
}
}

}
Output:

indhu
sindhu
swathi
null

null



ArrayList:

  • ArrayList is not Synchronized.
  • Arraylist also allows Duplicate Elements.
  • ArrayList allows n number of null values.
  • Arraylist  having insertion order with index.
  • For retrieve purpose ArrayList is best choice.
  • ArrayList having Randaom Access Nature.
Program for ArrayList ;



package com.instanceofjava;
import java.util.ArrayList;
import java.util.ListIterator;

import java.util.List;



public class A{

public static void main(String[] args) {


             ArrayList arrayList=new ArrayList();




arrayList.add("sindhu");

arrayList.add("sindhu");

arrayList.add("swathi");

arrayList.add(null);

arrayList.add(null);
ListIterator it=arrayList.listIterator();
while(it.hasNext()){
System.out.println(it.hasNext());
}
}
}

Output:
sindhu
sindhu
swathi
null
null

LinkedList:


  • LinkedList is not synchronized.
  • LinkedList allows n number of null values.
  • LinkedList allows Duplicate Elements.
  • LinkedList  having insertion order with index.
  • Insertion and Deletion purpose LinkedList is better choice.
  • LinkedList Follows Doubly linked list Structure.
Program for LinkedList:


package com.instanceofjava;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.List;

public class A{

public static void main(String[] args) {

  LinkedList linkedList=new LinkedList();




linkedList.add("sindhu");

linkedList.add("sindhu");

linkedList.add("swathi");

linkedList.add(null);

linkedList.add(null);

ListIterator it=linkedList.listIterator();
while(it.hasNext()){
System.out.println(it.hasNext());
}
}
}

Output:


sindhu
sindhu
swathi
null
null
  • In List interface if you want  to add elements we can use add() method.
  • If you want  to remove elements we can use remove() method.







Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Collections List

  1. Put more interview question and
    Answer about hibernate and spring

    ReplyDelete

Select Menu