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("===========================");
       }

}

1 comment:

  1. Wonderful website along with good subject as well. I currently added this great site in my bookmark page.

    ReplyDelete