Collections
Mr. Madar Bandu
                 The Collection in Java is a framework that provides an
                 architecture to store and manipulate the group of objects.
                 Java Collections can achieve all the operations that you
                 perform on a data such as searching, sorting, insertion,
Collections...   manipulation, and deletion.
                 Java Collection means a single unit of objects. Java
                 Collection framework provides many interfaces (Set, List,
                 Queue, Deque) and classes (ArrayList,
                 Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet,
                 TreeSet).
                   List interface is the child interface
                  of Collection interface. It inhibits a
                 list type data structure in which we      List interface is implemented by
                  can store the ordered collection of      the classes ArrayList, LinkedList.
                      objects. It can have duplicate
                                   values.
List Interface
                             List <data-type> list1= new ArrayList();
                             List <data-type> list2 = new LinkedList();
            The ArrayList class implements the List
            interface. It uses a dynamic array to store the
            duplicate element of different data types.
            The ArrayList class maintains the insertion
            order and is non-synchronized.
ArrayList
            The elements stored in the ArrayList class
            can be randomly accessed
            • boolean add(Object o)
            • int size()
            • void clear()
            • Object get(int index)
            • int indexOf(Object o)
ArrayList   • int LastIndexOf(Object o)
Methods     • Object remove(int index)
            • Bolean remove(Object o)
    ArrayList...
import java.util.*;
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
             LinkedList implements the Collection
             interface. It uses a doubly linked list
             internally to store the elements.
             It can store the duplicate elements. It
LinkedList   maintains the insertion order and is
             not synchronized.
             In LinkedList, the manipulation is fast
             because no shifting is required.
             •   boolean add(Object o)
             •   int size()
             •   void addFirst(Object o)
             •   void addLast(Object o)
             •   void clear()
             •   Object get(int index)
             •   Object getFirst()
LinkedList   •   Object getLast()
             •   int indexOf(Object o)
             •   int LastIndexOf(Object o)
             •   Object remove(int index)
             •   Bolean remove(Object o)
             •   Object removeFirst()
             •   Object removeLast()
    LinkedList
import java.util.*;
public class TestJavaCollection2{
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
            Queue interface maintains the first-in-first-out order. It
            can be defined as an ordered list that is used to hold
            the elements which are about to be processed.
            There are various classes like PriorityQueue, Deque,
            and ArrayDeque which implements the Queue
Queue       interface.
Interface   Queue<String> q1 = new PriorityQueue();
            Queue<String> q2 = new ArrayDeque();
                The PriorityQueue class implements
                the Queue interface. It holds the
                elements or objects which are to be
                processed by their priorities.
PriorityQueue
                PriorityQueue doesn't allow null
                values to be stored in the queue.
    PriorityQueue
import java.util.*;
public class TestJavaCollection5{
public static void main(String args[]){
PriorityQueue<String> queue=new PriorityQueue<String>();
queue.add("Amit Sharma");
queue.add("Vijay Raj");
queue.add("JaiShankar");
queue.add("Raj");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements:");
Iterator<String> itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
                Set Interface in Java is present in java.util package. It extends
                the Collection interface.
                It represents the unordered set of elements which doesn't
                allow us to store the duplicate items.
                We can store at most one null value in Set. Set is
                implemented by HashSet, LinkedHashSet, and TreeSet.
Set Interface
                Set<data-type> s1 = new HashSet<data-type>();
                Set<data-type> s2 = new LinkedHashSet<data-type>();
                Set<data-type> s3 = new TreeSet<data-type>();
          HashSet class implements Set
          Interface. It represents the
          collection that uses a hash
          table for storage.
HashSet
          Hashing is used to store the
          elements in the HashSet. It
          contains unique items.
HashSet...
import java.util.*;
public class TestJavaCollection7{
public static void main(String args[]){
//Creating HashSet and adding elements
HashSet<String> set=new HashSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} }
}
                LinkedHashSet class represents the
                LinkedList implementation of Set
                Interface.
                It extends the HashSet class and
LinkedHashSet   implements Set interface. Like HashSet,
                It also contains unique elements.
                It maintains the insertion order and
                permits null elements.
LinkedHashSet...
import java.util.*;
public class TestJavaCollection8{
public static void main(String args[]){
LinkedHashSet<String> set=new LinkedHashSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
          TreeSet class implements the Set
          interface that uses a tree for
          storage. Like HashSet, TreeSet
          also contains unique elements.
TreeSet
          However, the access and
          retrieval time of TreeSet is quite
          fast. The elements in TreeSet
          stored in ascending order.
TreeSet...
import java.util.*;
public class TestJavaCollection9{
public static void main(String args[]){
//Creating and adding elements
TreeSet<String> set=new TreeSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Any Queries?