Tuesday, May 10, 2011

Collection FrameWork in Java


Concrete Collections
--------------------

Concrete Collection     Implements  description

HashSet       Set                     Hashtable
TreeSet        SortedSet           Balance binary Tree
ArrayList      List                    Re-sizable array
LinkedList    List                    LinkedList
Vector          List                    re-sizable array
HashMap     Map                   hashtable
TreeMap      SortedMap        balance binary tree
HashTable    Map                  hashtable



/**
 *
 */
package com.jit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

/**
 * @author Vinay Guntaka
 * Factors of Collection Framework to be considered
 * API
 * Synchronization
 * Data growth
 * Usage patterns
 */
public class CollectionFrameWork {

    public static void main(String[] args) {
        getHashSet();
        getTreeSet();
        getArrayList();
        getLinkedList();
        getVectorList();
        getHashMap();
        getHashTable();
        getTreeMap();
       
    }
   
    /*
     * Unordered (A HashSet use the hash function to store the values)
     * No Duplicates
     * Allow Null
     */
   
    public static void getHashSet()
    {
        Set<String> hs = new HashSet<String>();
        hs.add("Vinay");
        hs.add("Vinay");
        hs.add("Kumar");
        hs.add("Guntaka");
        hs.add(null);
        System.out.println(hs);
    }
    /*  A TreeSet is more efficient in case of search element.
     *  null throws NullPointerException
     *  By Default Sorted
     *  No Duplicates
     */
    public static void getTreeSet()
    {
        Set<String> ts = new TreeSet<String>();
        ts.add("Vinay");
        ts.add("Vinay");
        ts.add("Kumar");
        ts.add("Guntaka");
        ts.add("");
        System.out.println(ts);
       
    }
   
    /**
     * If you need to support random access, without inserting or removing
     * elements from any place other than the end, then ArrayList offers the optimal collection.
       * If, however, you need to frequently add and remove elements from the middle of the list and
     * only access the list elements sequentially, then LinkedList offers the better implementation.
     *
     */
   
    /*
     * Ordered
     * Allows Null
     * Allows Duplicates
     * Iterating from To-And-Fro Directions
     * Not Synchronized
     */
   
    public static void getArrayList(){
        List<String> al= new ArrayList<String>();
        al.add("Vinay");
        al.add(null);
        al.add("Vinay");
        al.add("Guntaka");
        System.out.println(al);
        // Iterating from Back Side
        ListIterator<String> lsitr = al.listIterator(al.size());
        while(lsitr.hasPrevious()){
            System.out.println(lsitr.previous());
        }
        // Iterating from front Side
        while(lsitr.hasNext()){
            System.out.println(lsitr.next());
        }
        al.subList(0, 2).clear();
        System.out.println(al);
    }
    /**
     * In a LinkedList, each element is linked to its previous and next element making
     * it easier to delete or insert in the middle of the list
     */
   
    public static void getLinkedList(){
        List<String> ll= new LinkedList<String>();
        ll.add("Vinay");
        ll.add(null);
        ll.add("Vinay");
        ll.add("Guntaka");
        System.out.println(ll);
       
    }
   
    /*
     * Synchronized
     */
    public static void getVectorList(){
        Vector<String> v = new Vector<String>();
        v.add("Vinay");
        v.add("Kumar");
        v.add(null);
        v.add("Guntaka");
        v.add("Guntaka");
        System.out.println(v);
       
    }
    /*
     * Key Name should be Unique
     * Unidentified Order
     * UnSynchronized
     * The values can be null.
     */
   
    public static void getHashMap(){
        Map<String,String> hm = new HashMap<String, String>();
        hm.put("FName", "Vinay");
        hm.put("MName", "Kumar");
        hm.put("LName", "Guntaka");
        hm.put("MName", null);
        System.out.println(hm);
    }
    /*
     * Key Name should be Unique
     * Does not let you have null value for key.
     * Synchronized
     */
    public static void getHashTable(){
        Map<String,Integer> ht = new Hashtable<String, Integer>();
        ht.put("FName", 100);
        ht.put("MName", 99);
        ht.put("LName", 101);
        ht.put("Name", 152);
        System.out.println(ht);
    }
    /*
     * The values can be null. But the key should be unique
     * UnSynchronized
     */
    public static void getTreeMap(){
        Map<String,Integer> tm = new TreeMap<String, Integer>();
        tm.put("FName", 100);
        tm.put("MName", 99);
        tm.put("LName", 101);
        tm.put("Name", null);
        System.out.println(tm);
    }

}

No comments:

Post a Comment

Java 1.7 New Features Over 1.6

Automatic Resource Management Description: A proposal to support scoping of resource usage in a block with automatic resource cleanup. T...