Collections in Java
Collections in Java
Collections
• Collections in java is a framework that provides an architecture to store
and manipulate the group of objects.
• All the operations that you perform on a data such as searching, sorting,
insertion, manipulation, deletion etc. can be performed by Java
Collections.
• Java Collection simply means a single unit of objects. Java Collection
framework provides many interfaces (Set, List, Queue, Deque etc.) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet etc).
• What is Collection in java?
• Java ArrayList allows random access because array works at the index
basis.
• In Java ArrayList class, manipulation is slow because a lot of shifting needs
to be occurred if any element is removed from the array list.
•
Example of Java ArrayList class
import java.util.*;
class TestCollection1{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();//creating arraylist
al.add("Ravi");//adding object in arraylist
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Ravi Vijay Ravi Ajay
Java LinkedList class
• Java LinkedList class uses doubly linked list to store the elements. It
extends the AbstractList class and implements List and Deque
interfaces.
• Java LinkedList class can contain duplicate elements.