import java.util.*;
//------------------------------------------------------------------------------
//Creating and using Maps without generics. Map elements have key/value pairs 
//------------------------------------------------------------------------------
@SuppressWarnings("unchecked")

public class map1 
{
    public static void main(String[] args)
    {
//      TreeMap<String,String> Tmap = new TreeMap<String,String>(); //using generics
        TreeMap                Tmap = new TreeMap();                //without generics

        Tmap.put("Iam", "Sam");                     //add key/value pairs
        Tmap.put("son","Mike");
        Tmap.put("dog","Max");
                                                    //will print in sort order of keys
        System.out.println(Tmap);                   // {Iam=Sam, dog=Max, son=Mike}

        Object s  = Tmap.put("dog","Spot");         //change the value of dog to Spot
                                                    //return Max (the previous value)
        Object s2 = Tmap.get("dog");
        System.out.println(s2);                     //prints Spot
        System.out.println(Tmap);

        //iterating through a map, One way -------------------------------

        Set keys       = Tmap.keySet();             //get all the keys as a Set
        Object[] keys2 = keys.toArray();            //convert the Set to an Array

        for (int i=0; i<keys2.length; i++)          //or use keys.size() 
        {
            String k = (String) keys2[i];           //cast Object to a String
            String v = (String) Tmap.get(k);
            System.out.println(k + " has value= " + v);
        }

        //iterating through a map, Another way --------------------------

        for (Object entry : Tmap.entrySet())            //iterate thru the map
        {
            System.out.print(entry + " \t");            //prints key=value 

            String kv = entry.toString();               //convert to String
            String[] array = kv.split("=");             //split on the =
            String k  = array[0];                       //the key component
            String v  = array[1];                       //the value component
            System.out.println(k + " has value= " + v);
        }
    }
}