Collection Framework
Collection Framework
Collection Framework
Collection Framework
Collections in Java
The Iterable interface is the root interface for all the collection
classes. The Collection interface extends the Iterable interface
and therefore all the subclasses of Collection interface also
implement the Iterable interface.
It contains only one abstract method. i.e.,
Iterator<T> iterator()
It returns the iterator over the elements of type T.
Collection Interface
1.import java.util.*;
The ArrayList class 2.class TestJavaCollection1{
implements the List interface. 3.public static void main(String args[]){
4.ArrayList<String> list=new ArrayList<String
It uses a dynamic array to
>();//Creating arraylist
store the duplicate element
5.list.add("Ravi");//Adding object in arraylist
of different data types. The 6.list.add("Vijay");
ArrayList class maintains the 7.list.add("Ravi");
insertion order and is non- Output:
8.list.add("Ajay");
synchronized. The elements 9.//Traversing list through Iterator
stored in the ArrayList class Ravi
10.Iterator itr=list.iterator();
Vijay
can be randomly accessed. 11.while(itr.hasNext()){
Ravi
Consider the following 12.System.out.println(itr.next());
Ajay
example. 13.}
14.}
15.}
LinkedList
1.import java.util.*;
2.public class TestJavaCollection2{
LinkedList implements the 3.public static void main(String args[]){
Collection interface. It uses a 4.LinkedList<String> al=new LinkedList<String>()
doubly linked list internally ;
to store the elements. It can 5.al.add("Ravi");
store the duplicate 6.al.add("Vijay");
elements. It maintains the 7.al.add("Ravi");
8.al.add("Ajay"); Output:
insertion order and is not
9.Iterator<String> itr=al.iterator();
synchronized. In LinkedList, 10.while(itr.hasNext()){ Ravi
the manipulation is fast 11.System.out.println(itr.next()); Vijay
because no shifting is 12.} Ravi
required. 13.} Ajay
14.}
Vector
1.import java.util.*;
Vector uses a dynamic array 2.public class TestJavaCollection3{
to store the data elements. It 3.public static void main(String args[]){
4.Vector<String> v=new Vector<String>();
is similar to ArrayList.
5.v.add("Ayush");
However, It is synchronized 6.v.add("Amit");
and contains many methods 7.v.add("Ashish");
that are not the part of 8.v.add("Garima");
Collection framework. Output:
9.Iterator<String> itr=v.iterator();
10.while(itr.hasNext()){
Ayush
11.System.out.println(itr.next());
Amit
12.}
Ashish
13.}
Garima
14.}
Stack
1.import java.util.*;
2.public class TestJavaCollection4{
3.public static void main(String args[]){
The stack is the subclass of 4.Stack<String> stack = new Stack<String>();
Vector. It implements the 5.stack.push("Ayush");
last-in-first-out data 6.stack.push("Garvit");
structure, i.e., Stack. The 7.stack.push("Amit");
stack contains all of the 8.stack.push("Ashish");
methods of Vector class and 9.stack.push("Garima");
10.stack.pop(); Output:
also provides its methods
11.Iterator<String> itr=stack.iterator();
like boolean push(), boolean 12.while(itr.hasNext()){ Ayush
peek(), boolean push(object 13.System.out.println(itr.next()); Garvit
o), which defines its 14.} Amit
properties. 15.} Ashish
16.}
Queue Interface