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:
- 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.
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.
Put more interview question and
ReplyDeleteAnswer about hibernate and spring