Wednesday, 10 September 2014

Map Interface Examples

package com.ram;

import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * @author ramakrishna.v
 *
 */
public class MapInterfaceExamples {
   
    public static void main(String args[]) {
       
        Map<String,String> hashMap = new HashMap<String,String>();
        hashMap.put("key1","R");
        hashMap.put("key2","A");
        hashMap.put("key3","M");
       
        //Getting all keys
        retrieveValues("Map","HashMap",hashMap.keySet());
        //Getting all values
        retrieveValues("Map","HashMap",hashMap.values());
        //Getting all values based on keys
        getValues("Map","HashMap",hashMap,hashMap.keySet());
        //Getting all values based on keys getting by set interface
        displayValues("Map","HashMap",hashMap,hashMap.entrySet());
         
        Map<String,String> treeMap1 = new TreeMap<String,String>();
        treeMap1.put("key1","R");
        treeMap1.put("key2","A");
        treeMap1.put("key3","M");
       
        //Getting all keys
        retrieveValues("Map","TreeMap",treeMap1.keySet());
        //Getting all values
        retrieveValues("Map","TreeMap",treeMap1.values());
        //Getting all values based on keys
        getValues("Map","TreeMap",treeMap1,treeMap1.keySet());
        //Getting all values based on keys getting by set interface
        displayValues("Map","TreeMap",treeMap1,treeMap1.entrySet());
       
        SortedMap<String,String> treeMap2 = new TreeMap<String,String>();
        treeMap2.put("key1","R");
        treeMap2.put("key2","A");
        treeMap2.put("key3","M");
       
        //Getting all keys
        retrieveValues("SortedMap","TreeMap",treeMap2.keySet());
        //Getting all values
        retrieveValues("SortedMap","TreeMap",treeMap2.values());
        //Getting all values based on keys
        getValues("Map","TreeMap",treeMap2,treeMap2.keySet());
        //Getting all values based on keys getting by set interface
        displayValues("Map","TreeMap",treeMap2,treeMap2.entrySet());
         
        Map<String,String> linkedHashMap = new LinkedHashMap<String,String>();
        linkedHashMap.put("key1","R");
        linkedHashMap.put("key2","A");
        linkedHashMap.put("key3","M");
       
        //Getting all keys
        retrieveValues("Map","LinkedHashMap",linkedHashMap.keySet());
        //Getting all values
        retrieveValues("Map","LinkedHashMap",linkedHashMap.values());
        //Getting all values based on keys
        getValues("Map","LinkedHashMap",linkedHashMap,linkedHashMap.keySet());
        //Getting all values based on keys getting by set interface
        displayValues("Map","LinkedHashMap",linkedHashMap,linkedHashMap.entrySet());
       
        Map<String,String> hashtable = new Hashtable<String,String>();
        hashtable.put("key1","R");
        hashtable.put("key2","A");
        hashtable.put("key3","M");
       
        //Getting all keys
        retrieveValues("Map","LinkedHashMap",hashtable.keySet());
        //Getting all values
        retrieveValues("Map","LinkedHashMap",hashtable.values());
        //Getting all values based on keys
        getValues("Map","hashtable",hashtable,hashtable.keySet());
        //Getting all values based on keys getting by set interface
        displayValues("Map","hashtable",hashtable,hashtable.entrySet());
    }
   
     @SuppressWarnings({ "rawtypes"})
     static void retrieveValues(String var1,String var2,Collection collection) {
         System.out.println(var1+" interface using "+var2+" class");
          Iterator iterator = collection.iterator();
          while (iterator.hasNext()) {
             String string = (String) iterator.next();
             System.out.println(string);
          }
          System.out.println("===========================");
       }
   
     @SuppressWarnings({ "rawtypes"})
     static void getValues(String var1,String var2,Map map,Collection collection) {
         System.out.println(var1+" interface using "+var2+" class");
          Iterator iterator = collection.iterator();
          while (iterator.hasNext()) {
             String key = (String) iterator.next();
             String value = (String)map.get(key);
             System.out.println("key:"+key+" value:"+value);
          }
          System.out.println("===========================");
       }
   
     @SuppressWarnings({ "rawtypes"})
     static void displayValues(String var1,String var2,Map map,Collection collection) {
         System.out.println(var1+" interface using "+var2+" class");
       
         Set set = (Set) collection;
          Iterator iterator = set.iterator();
          while (iterator.hasNext()) {
              Map.Entry entry = (Map.Entry) iterator.next();
              String key = (String) entry.getKey();
              String value = (String) entry.getValue();
             System.out.println("key:"+key+" value:"+value);
          }
          System.out.println("===========================");
       }

}

Set Interface Examples

package com.ram;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 *
 * @author ramakrishna.v
 *
 */
public class SetInterfactExamples {
   
    public static void main(String args[]) {
       
            Set<String> hashSet = new HashSet<String>();
            hashSet.add("a");
            hashSet.add("b");
            hashSet.add("c");
            retrieveValues("Set","HashSet",hashSet);
           
            Set<String> treeSet1 = new TreeSet<String>();
            treeSet1.add("R");
            treeSet1.add("A");
            treeSet1.add("M");
            retrieveValues("Set","TreeSet",treeSet1);
           
            SortedSet<String> treeSet2 = new TreeSet<String>();
            treeSet2.add("R");
            treeSet2.add("A");
            treeSet2.add("M");
            retrieveValues("SortedSet","TreeSet",treeSet2);
           
            Set<String> linkedHashset = new LinkedHashSet<String>();
            linkedHashset.add("R");
            linkedHashset.add("A");
            linkedHashset.add("M");
            retrieveValues("Set","LinkedHashSet",linkedHashset);
    }
   
    @SuppressWarnings({ "rawtypes"})
     static void retrieveValues(String var1,String var2,Collection collection) {
         System.out.println(var1+" interface using "+var2+" class");
          Iterator iterator = collection.iterator();
          while (iterator.hasNext()) {
             String string = (String) iterator.next();
             System.out.println(string);
          }
          System.out.println("===========================");
       }
}

List Interface Examples

package com.ram;

import java.util.Collection;
import java.util.Iterator;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
import java.util.Vector;

/**
 *
 * @author ramakrishna.v
 *
 */
public class ListInterfaceExamples {

    public static void main(String[] args) {
       
        List<String> arayList = new ArrayList<String>();
        arayList.add("R");
        arayList.add("A");
        arayList.add("M");
        retrieveValues("List","ArrayList",arayList);
       
        List<String> linkedList = new LinkedList<String>();
        linkedList.add("R");
        linkedList.add("A");
        linkedList.add("M");
        retrieveValues("List","LinkedList",linkedList);
       
        List<String> stack = new Stack<String>();
        stack.add("R");
        stack.add("A");
        stack.add("M");
        retrieveValues("List","Stack",stack);
       
        List<String> vector = new Vector<String>();
        vector.add("R");
        vector.add("A");
        vector.add("M");
        retrieveValues("List","Vector",vector);
       
       }
   
     @SuppressWarnings({ "rawtypes"})
     static void retrieveValues(String var1,String var2,Collection collection) {
         System.out.println(var1+" interface using "+var2+" class");
          Iterator iterator = collection.iterator();
          while (iterator.hasNext()) {
             String string = (String) iterator.next();
             System.out.println(string);
          }
          System.out.println("===========================");
       }
    }