COLLECTION FRAMWORK
Collection Framework: Its an api which set of classes and interfaces
Which provides the readymade architecture for data structures like
list, set, queue.
Framework: It is set of classes and interfaces which provides a
readymade architecture.
Iterable: Iterable is a root interface for the entire collection
framework. The collection interface extends the Iterable interface.
Therefore, all the classes and interfaces and classes implements this
interface. the main functionality of this interface is to provide an
iterator for the collection.
Collections: It is utility class .
Collections is a framework provided by java, this framework provides
many interfaces and their implemented classes in order to store a
group of objects in a single entity. All the collections are in
java.util.package.
Collection : It contains all the basic methods which every collection
has like adding the data into the collection removing the data
creating data ect.
What is the difference between Arrays and Collections?
              Arrays                            Collections
   1. They has fixed size            1.Collections has dynamic size
   2. Can        store     only      2.can store heterogeneous
      homogeneous type of data       collection of data.
   3. Arrays doesn’t support         3.Collectons support generics.
      generics                       4. Collections have a rich set of
   4. Arrays doesn’t have any        methods to perform operations
      supportive methods to          on collections.
      perform operations.
  1) LIST : List is an interface which provides the facility to store a
     group of objects in a single entity, where insertion allowed
     based on index, null can be inverted, duplicates are allowed.
     List is good: to store and retrieval of data.
List is implemented by 3 classes:
   1.Vector----> extended by stack
   2.Arraylist
   3.Linkedlist
1.Vector :
Vector is a legacy class. It provides a facility to store
a group of objects in a single entity. vector is thread
safe class so suitable for multithreaded environment so
the process of vector will be slow.
class VectorTest
{
     static void show()
     {
          //create
          Vector<String> v1 = new Vector<>() ;
          v1.add("Pavithra");
          v1.add("Sahana");
          v1.add("karuna");
          v1.add("sapna");
          v1.add("madhu");
         System.out.println(v1);
         //delete
         v1.remove("madhu");
         v1.remove(2);
         v1.removeAll(v1);
         v1.clear();
         System.out.println(v1);
         System.out.println(v1.capacity());//10
         System.out.println(v1.size()); //3--no of
elements present
         Vector<Object> v2= new Vector<>();
         v2.add("kavya");
         v2.add("preeti");
         v2.add("sandhya");
         v2.add(v1); //if genetics applied to v2 this
will not be allowed
         System.out.println(v2);
         v2.add(2,v1);
         System.out.println(v2.remove(v1)); // stores all
the elements of v1 as an single object
         System.out.println(v2);
         //Retrieve
         System.out.println(v2.get(3));
         //remove duplicate elements
         Vector<String> v3 = new Vector<>();
         v2.add("kavya");
         v2.add("preeti");
         v2.add("sandhya");
         /*for(Object st : v2)
         {
              if(v3.contains(st)==false)
              {
                   v3.add((String) st);
              }
         }*/
         v2.add("sahana");
         v2.set(0, "pratibha");
         v2.set(0, "karunya");
         System.out.println(v2);
         int a[]= {2,3,4,1,8};
         Vector< Integer> obj = new
Vector(Arrays.asList(a)); //converting array to list
         //conver arrayList to array
         Object a2[] = obj.toArray();
    }
}
public class VectorsDemo {
    public static void main(String[] args) {
         VectorTest.show();
    }
2.ArrayList: Array list provides a facility to store a group
of objects in a single entity. And the object of an
arraylist supports duplicate elements & stores elements
based on index
And insertion order.
Array list is best for store and retrieval of elements.
Ex : public class ArrayListDemo {
    public static void main(String[] args) {
//add
           ArrayList<String> obj = new ArrayList<>();
                obj.add("rama");
               obj.add("suma");
           obj.add(0,"vanitha");
           obj.add("sadana");
           System.out.println(obj);
         obj.get(3);
//retrieve
         System.out.println(obj.get(1));
           System.out.println(obj.contains("sadana"));
Verify
           System.out.println(obj.set(1, "madhu"));
//update
           System.out.println(obj);
What is the difference between Array list and              vector?
               Vector                              Array List
1.vector is a legacy class            1.ArrayList is not a legacy class
implements list interface.            but implements list interface.
2.It is a thread safe multiple        2.Arraylist is not a thread safe
threads cannot be handled             multiple threads can be handled
simultaneously, so it is a            simultaneously, so it is not
synchronized one by default.          synchronized one by default.
                                      3.Arraylist is more efficient than
3.so vector performance is slow       vector.
4.Vector grows doubling its           4.AL grows 50% of its current
current size, so it is suitable for   size, so it is more efficient for
where huge amount of data             memory usage.
increases exponentially.
3.Stacks : Stack is a class which extends vector class and list
interface and provides a facility to store a group of objects as a single
entity. It represents the last in first out (LIFO) principle.
Suitable for – redo, undo, last saved transactions, browser history.
special methods availabale in stacks
   push () – adds an element at top of the list
   pop () -- removes the top most element
   peek () – shows top most element
   search () – finds the element.
   IndexOf () --
public class StacksDemo {
     public static void main(String[] args) {
           Stack<String> book = new Stack<>();
           book.push("java");     //add to stack
           book.push("c++");
           book.push("c");
           book.push("networks");
           book.push("c#");
           book.pop();       //remove top element
           book.peek();        //view top most element
           book.remove(0);      //remove element based on index
           book.add("card");
           System.out.println(book.search("c"));
           System.out.println(book.indexOf("c"));
Linked list:   linked list extends list and dequeuer interfaces
Linked list allows to store a group of objects in a single entity.
internally it stores in the form of nodes, each node contains the
address of previous node, item and address of the next node.
It allows duplicates and follows insertion order, null can be accepted.
Linked list doesn’t create memory until inserting an element .
It is best for data manipulating i.e. add or delete element from
linked list, because there is no shifting operation takes place.
package com.gentech.collections;
import java.util.LinkedList;
public class LinkeListDemo {
     public static void main(String[] args) {
           LinkedList<String> l = new LinkedList<>();
           l.add("sindhu"); //add elements
           l.add("pavan");
           l.add("navya");
           l.add("balu");
           l.add("abhi");
           l.add("tanu");
           l.remove("abhi");     //remove element
     l.clear(); // remove elements        -- first and last node
//makes null so that all the elements goes into garbage
collection
l.contains("pavan"); //verify
l.set(0,"sainath") // update
System.out.println(l.get(0));
     }
       Vector  ArrayLi Stack Linked Priorit      ArrayD   Hash Linked   Tree
                          st     s    List   yQ      Q       set    hashs    set
                                                                     et
Default cap?
                 10       0     10     0     11     17       0       16      0
Initial cap?
                 10       10    10     0     11     17       16      16      0
 Duplicate
 element?
                 Yes     Yes    Yes   Yes    Yes    Yes     No       No     No
 Allow null
   Value?
                 Yes     Yes    Yes   Yes    No     No      Yes     Yes     No
 Maintains
 insertion       Yes     Yes    Yes   Yes    Yes    No      No      Yes     No
   order?
   Sorted
   order?
                 No      No     No    No     No     No      No       No     Yes
  Random                                                             No
  access?        Yes     Yes    Yes   Yes    No     No      No              No
Synchronize                    Yes(
    d?                          but
                                we
                                                    No      No       No     No
                 Yes     No    shou   Yes    no
                                 ld
                                not
                               use)
  What is                             Best                                  Uses
 good for?                                                  Uses
                                       for          Data                    tree
               Multithr               data
                                                            hash
                                                   manup                    map
                eading,              manu Heigh ulation     map
                           To                                       Uses    and
               Best for              pulati priori can be     to
                         store                                      linke   follo
                 when                on like                 stor
                          and                 ty    done              d      ws
                data is         LIFO add,                      e
                        retreiv      remov first    from            hash    bina
               exponen                                      data
                            e        e. Bcz  out    both            map ,    ry
                 tially                                     inter
                                       no            the                    stru
               growing               shiftin
                                                            nally
                                                   sides.                   ctur
                                        g                      .
                                                                             e.
Queue: Queue is an interface which extends list interface. And
allows to store a group of data as a single entity First in First out
principle (FIFO). here duplicates are allowed but not null, and
random access of elements is not allowed.
Mainly categorized into 2 types
a. Priority queue.
b. Arraydequeue
  a. Priority queue: It is a class implements queue interface. It
     works based on the higher priority first out principle(HPFO).
  Here duplicates are allowed and null is not inverted, it maintains
  the insertion order, random access cannot be done.
  Applicable in hospital queue, ticket reservation system.
  Methods:
  Peek (): shows the head element (head element means higher
  prioritized element)
  Poll (): we can remove the head element.
  b. Arraydequeue: It is a special kind of queue that allows to do
     data manipulation from both the sides that is adding or
     removing.
     Suitable for where efficient insertion and deletion is done.
     Supportive methods are
     Addition: offerFirst(), offerLast(), addFirst(), addLast().
     Retrieve: peek(), peekFirst(), peekLast().
     Removal: poll(), pllFirst(), pollLast().
     Set:   set is an interface which extends the collection interface.
     And it allows to store only a unique set of data. Set internally
     implements map.
Hash set: the object of hashmap stores the elements randomly in
memory using hash map (array of nodes(key and value pair)). If we
do not provide any value by default it creates a dummy object of
object and doesn’t allow duplicates.
Note: updation cannot be allowed here.
Linked Hash set:
Tree set: tree set internally uses tree map binary structure to store
the elements. The object of tree set stores the elements based on
sorted assending order.
Note: at any point the object of tree set never support without
approach of generics object creation.
TreeSet<Integer> t = new TreeSet<>();
         t.add(12);
           t.add(10);
           t.add(20);
           t.add(13);
           t.add(9);
           System.out.println(t); // [9, 10, 12, 13, 20]
         System.out.println(t.first()); //returns first/
highest element
         System.out.println(t.last()); //returns last/
lowest element
         System.out.println(t.pollFirst()); // removes
first element
           System.out.println(t.pollLast()); // removes last
element
         System.out.println(t.subSet(12, 20)); // returns
elements between 12 to 20
         //creates a virtual set . operations performed on
subset reflects on main set.
         System.out.println(t.subSet(12,20).removeAll(t));
// removes between 12 to 20 in main set
MAP :      map is an interface, it provides a facility to store a group
of objects as a single entity in the form of key and value pair. Here
key always must be unique one.
When we prefer map ?
List is not applicable for huge amount of data because it is difficult to
remember all the ids and also list iterates every time so its
performance will be slow. In order to overcome this we prefer map .
There are 2 main maps
a. Hash map
b. Tree map
a. Hash map: Object of hash map supports key and value pair of
adding elements. Here key should be unique, it stores keys in the
memory randomly.
           Map<Integer, String> m = new Hashtable<>();
           m.put(1, "bengaluru"); //adding elements
           m.put(2,"chennai");
           m.put(3, "hydrabad");
           m.put(4, "delhi");
           m.put(5, "delhi");
           System.out.println(m);
           System.out.println(m.keySet());    // returns only
the keys
           System.out.println(m.values());    // returns only
values
           //iterate keys
           Set<Integer> key = m.keySet();
           for(Integer k:key)
           {
                System.err.println(k);
           }
           //iterate values
           Collection<String> value = m.values();
           for(String v : value)
           {
                System.err.println(v);
           }
           //get value based on key
           System.out.println(m.get(4));
           for(Integer k:key)
           {
                System.err.println(k+ " ==> "+m.get(k));
           }
           //remove element
           System.out.println(m.remove(4));
           //verification
         System.out.println(m.containsKey(4)); //returns
boolean value
           System.out.println(m.containsValue("delhi"));
           Treemap :