Thursday, 16 June 2016

Collections in java

Java Collections:
Collection: Collection represents a single unit of object/group.
Java Collections framework is collection of interfaces and classes which helps in storing and procesing data efficiently.

                        Collection(interface)
Set(interface)                        List(interface)                Queue(interface)
Hashset,LinkedHashSet,Treeset(sortedSet)        ArrayList,Vector,LinkedList        LinkedList,PriorityQueue

1) List:
List is an ordered collection(sequence).
List may contains duplicate elements
List index is starts with Zero

1a) ArrayList:
ArrayList is resizable implemented from List interface
Java ArrayList class can contain duplicate elements.
ArrayList class maintain insertion order.
ArrayList class is non synchronized.

Methods of ArrayList class:

add("String")
add(index, "String")
remove("String")
set(int index,Object)
indexOf(Object)
get(int index)
size()
contains("String")
clear()



Sample code using ArrayList:


package collections;

import java.util.ArrayList;

public class ArrayList_Example {

    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList<String>();
        arr.add("padi");
        arr.add("pravanjan");
        arr.add("reddy");
        System.out.println("printing arrayList: " + arr);
        System.out.println("Printing size of array:" + arr.size());
        System.out.println("Element at index 0 :" + arr.get(0));
        System.out.println("Does arrayList contains padi: "+arr.contains("padi"));
        arr.add(2, "REDDDY");
        System.out.println("printing arrayList: " + arr);
        System.out.println("Index of PERL is "+arr.indexOf("reddy"));
        System.out.println("Printing size of array:" + arr.size());
       
    }
}

Output:
printing arrayList: [padi, pravanjan, reddy]
Printing size of array:3
Element at index 0 :padi
Does arrayList contains padi: true
printing arrayList: [padi, pravanjan, REDDDY, reddy]
Index of PERL is 3
Printing size of array:4

1b) LinkedList:
Java v class uses doublly linked list to store the elements
LinkedList class can contain duplicate elements
LinkedList class maintains insertion order
LinkedList is non synchronized

Sample Code:
package collections;

import java.util.LinkedList;
import java.util.*;

public class LinkedList_Example {
   
    public static void main(String[] args) {
       
   
    LinkedList<String> ll=new LinkedList<String>();
    ll.add("pabbu");
    ll.add("reddy");
    ll.add("aaaaaaaaaa");
    System.out.println("Printin all linked list values : " + ll);
    Iterator<String> itr=ll.iterator();
    while (itr.hasNext()) {
    System.out.println("printing values using iterator :" + itr.next());
       
    }
    }
}

Output:
Printin all linked list values : [pabbu, reddy, aaaaaaaaaa]
printing values using iterator :pabbu
printing values using iterator :reddy
printing values using iterator :aaaaaaaaaa

+++++++++++++++++ Hash Set Class++++++++++++++
HashSet class uses hashtable to store the elements, It extends AbstartctSet class implements Set interface
HashSet contains only unique elemets.
HashSet contains only Values
 Difference between List and Set:
 - List can contains duplicate elements whereas Set contains unique elemenets only.

++++++++++ Sample code using HashSet class +++++++++
package collections;

import java.util.HashSet;
import java.util.Iterator;

public class HashSet_Example {
public static void main(String[] args) {
    HashSet<String> hset=new HashSet<String>();
    hset.add("pabbu");
    hset.add("pabbu");
    hset.add("reddy");
    hset.add("abcd");
    System.out.println("Printin all values from HaseSet: " + hset);
    Iterator<String> itr=hset.iterator();
    while (itr.hasNext()) {
    System.out.println("printin vaue using iterator : " + itr.next());
       
    }
   
}
}

Output :
Printin all values from HaseSet: [pabbu, reddy, abcd]
printin vaue using iterator : pabbu
printin vaue using iterator : reddy
printin vaue using iterator : abcd

++++++++++++++++ Tree Set Class ++++++++++
- Contains uniqe elements like HashSet
 - Maintains ascending order

Sample code for TreeSet :

package collections;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSet_Example {
public static void main(String[] args) {
    TreeSet<String> ts=new TreeSet<String>();
    ts.add("pabbu");
    ts.add("abcd");
    ts.add("reddy");
    ts.add("reddy");
    System.out.println(" Printin all values from TreeSet: " + ts);
    Iterator<String> itr=ts.iterator();
    while(itr.hasNext())
    {
        System.out.println("printin all values using iterator :" + itr.next());
    }
   
   
}
}

Output :
 Printin all values from TreeSet: [abcd, pabbu, reddy]
printin all values using iterator :abcd
printin all values using iterator :pabbu
printin all values using iterator :reddy

+++++++++++++    HashMap               +++++++++++
 - HashMap contains values based on the key. It implements the Map interface and extends the AbstractMap class.
 - It contains only Uniq elements
 - It may have one null key and multiple null values.
 - It maintains no order.

Sample code for HashMap:
package collections;
import java.util.*;
import java.util.HashMap;
import java.util.Map;


public class HashMapTest {
    public static void main(String[] args) {
       
   
HashMap<Integer,String> hm=new HashMap<Integer,String>();
hm.put(100,"pabbu");
hm.put(200, "reddy");
hm.put(200, "reddy");

    for(HashMap.Entry m:hm.entrySet())
    {
        System.out.println(m.getKey() + " "+m.getValue());
       
    }
   
    }
}


Output:

100 pabbu
200 reddy

+++++++++++++++
What is difference between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry(key and value).

++++++    Hash Table    ++++++++++++++
- HashTable is an array of list. Each list is know as bucket. The position of bucket is identified by calling the hascode() method.
- HashTable contains values based on the Key.
- it contains only Uniq elements.
- It may not have any null key or value.
- It is Synchronized

Sample Code for HashTable:
package collections;

import java.util.Hashtable;
import java.util.Map;

public class HashTable_Test {
    public static void main(String[] args) {
        Hashtable<Integer,String> ht=new Hashtable<Integer,String>();
        ht.put(100,"pabbu");
        ht.put(200, "reddy");
        ht.put(300,"pabbu");
        ht.put(100,"abcd");
        ht.put(400," ");
       
       
        for(Map.Entry m:ht.entrySet())
                {
            System.out.println(m.getKey()+" "+m.getValue());
                }
    }

}

Output:
400 
300 pabbu
200 reddy
100 abcd


++++++++++     Diffrence between HashMap and Hashtable     ++++

    HashMap                                Hashtable
1) HashMap is non synchronized. It is not thread safe.        1) Hashtable is synchronized.It is thread safe and can be shaed with manu threads.
2) HashMap allows one null key and multiple null values        2) Hashtable doesnt allow any null key or value
3) HashMap is a new class introduced in JDK 1.2            3) Hashtable is legacy class
4) HashMap is fast                        4) hASHTABLE is slow
5) We can make HashMap as synchronized by calling
   Map m=Collections.synchronizedMap(hashMap)            5) Hashtable is internally syncronized






No comments:

Post a Comment