import java.util.*;
//------------------------------------------------------------------------------
//Creating and using Maps with generics. Map elements have key/value pairs 
//------------------------------------------------------------------------------
public class map2 
{
    public static void main(String[] args) 
    {
        //=== Example of using a HashMap =======================================
        //A HashMap returns elements in no particular order

        HashMap<Integer,String> Hmap = new HashMap<Integer,String>();
        String str;
                
        System.out.println("A HashMap: " + Hmap);

        Hmap.put(5, "Sam");
        Hmap.put(86,"Mike");
        Hmap.put(1, "John");
                                                                        
        System.out.println("A HashMap: " + Hmap);
        System.out.println();
        
        //iterating through a map, the hard way (without generics)--------------
        
        Set keys       = Hmap.keySet();                 //get all the keys as a set
        Object[] keys2 = keys.toArray();                //convert the set to array of Obj

        for (int i=0; i<keys2.length; i++)
        {
            Integer k = (Integer) keys2[i];             //cast each key to an Interger
            String  v = Hmap.get(k);
            System.out.println(k + " has the value: " + v);
        }
        System.out.println();

        //iterating through a map, an easier way (without generics)-------------
        //each Map.Entry object contains a key/value pair
        
        for (Map.Entry entry : Hmap.entrySet()) 
        {
            System.out.print(entry + "\t");                     //print each entry as k=v

            Integer k = (Integer) entry.getKey();               //get the key
            String  v = (String)  entry.getValue();             //get the value

            System.out.println(k + " has value=" + v);
        }
        System.out.println();
 


        //=== Example of using a TreeMap =======================================
        //A TreeMap always returns elements in sort order of value

        TreeMap<String,String> Tmap = new TreeMap<String,String>();
                
        System.out.println("A TreeMap: " + Tmap);

        Tmap.put("name", "Sam");                       
        Tmap.put("son",  "Mike");                      
        Tmap.put("dog",  "Max");                      
                                                                        
        System.out.println("A TreeMap: " + Tmap);       //prints in sort order 
        System.out.println();
        
        str = Tmap.put("dog", "Spot");                  //change the value of dog to spot
                                                        //str will be the previous value
        System.out.println("Prev value for element dog: " + str);

        str = Tmap.get("dog");                          //get element 1 

        System.out.println("New value for element dog: " + str);

        System.out.println(Tmap);
        System.out.println();

        //iterating through a map, the easy way (with generics)-----------------
        //each Map.Entry object is a key/value pair 

        for (Map.Entry<String,String> entry : Tmap.entrySet()) 
        {
            System.out.print(entry + "\t");                     //print each entry as k=v

            String k = entry.getKey();                          //no need to cast
            String v = entry.getValue();                        

            System.out.println(k + " has value=" + v);
        }       
        System.out.println();
    }
}