Iterable Interface - Root Interface of all collection classes.
Collection Interface - extends Iterable Interface.
List , Queue , and Set Interfaces extends Collection Interface.
List Interface In Java :
List can have duplicate values.
List interface is implemented by classes : ArrayList,LinkedList,Vector,Stack.
ArrayList :
ArrayList maintain insertion order and is non-synchronized , elements stored in ArrayList can be accessed randomly.It internally uses dynamic array,In ArrayList searching is fast because of indexing.
In below example we have created an ArrayList , added elements in it and iterated using for each loop and using iterator .
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CollectionDemo {
public static void main(String[] args) {
List<String> studentList= new ArrayList<String>();
studentList.add("Neel");
studentList.add("Pooja");
studentList.add("Shivam");
System.out.println("Displayed using For each loop");
for(String name:studentList) {
System.out.println(name);
}
Iterator myIterator=studentList.iterator();
System.out.println("Displayed using Iterator :");
while(myIterator.hasNext()) {
System.out.println(myIterator.next());
}
}
}
Output :
Displayed using For each loop
Neel
Pooja
Shivam
Displayed using Iterator :
Neel
Pooja
Shivam
LinkedList :
It uses doubly linked list internally,maintain insertion order and is not synchronized , in LinkedList Insertion,Deletion(that is , manipulation) is fast because no shifting is required.
import java.util.LinkedList;
import java.util.List;
public class LinkedListDemo {
public static void main(String[] args) {
List<String> subjectList= new LinkedList<String>();
subjectList.add("Java");
subjectList.add("C");
subjectList.add("CPP");
System.out.println("Displayed using For each loop");
for(String name:subjectList) {
System.out.println(name);
}
}
}
Displayed using For each loop
Java
C
CPP
Vector : uses dynamic array internally to store element , it is similar to ArrayList,difference is that , ArrayList is non-synchronized and Vector is synchronized.
import java.util.List;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
List<String> subjectList= new Vector<String>();
subjectList.add("A");
subjectList.add("B");
subjectList.add("C");
System.out.println("Displayed using For each loop");
for(String name:subjectList) {
System.out.println(name);
}
}
}
Output :
Displayed using For each loop
A
B
C
Stack :
Stack implements the last-in-first-out data structure, i.e., Stack. The stack is the subclass of Vector.
The stack contains all of the methods of Vector class and also provides its own methods like boolean push(), boolean peek(), boolean push(object o).
public class VectorDemo {
public static void main(String[] args) {
Stack<String> subjectList= new Stack<String>();
subjectList.push("A");
subjectList.push("B");
subjectList.push("C");
System.out.println("Displayed using For each loop");
subjectList.pop();
for(String name:subjectList) {
System.out.println(name);
}
}
}
Output :
Displayed using For each loop
A
B