// Example 4.4.1 joi/examples/TreeMap15Demo.java                         
//                                                            
//                                                            
// Copyright 2004 Bill Campbell and Ethan Bolker                         

import java.util.TreeMap;
import java.util.Map;
import java.util.Map.Entry;
                                                            
// A class illustrating the use of TreeMap in Java 1.5.  
// Functionality matches that in joi/examples/TreeMapDemo.java
//
// A typical run:
//
// %> java TreeMap15Demo
// Store 3 wrapped ints, keys "one", "two", "three".
// The wrapped int stored for "two" is 2
// 
// Iterate over keys, get each value.
// Note that key order is aphabetical:
// The value for key one is 1
// The value for key three is 3
// The value for key two is 2
// 
// Iterate over the values:
// 1
// 3
// 2
// 
// Iterate over the key-value pairs:
// The value for the entry with key one is 1
// The value for the entry with key three is 3
// The value for the entry with key two is 2
// 
// How a TreeMap represents itself as a String:
// {one=1, three=3, two=2}
// 
// Store a different value at key "two"
// {one=1, three=3, two=2222}
// 
// Store map.get( "one" ) at key "two"
// {one=1, three=3, two=1}
// 
// A TreeMap with Integer keys mapping to String values
// {1=I, 2=II, 3=III}
// %>

public class TreeMap15Demo
{
    public static void main( String[] args ) 
    {
        Terminal terminal = new Terminal(); // for input and output

	// Declare and create a TreeMap with String keys, Integer values
        TreeMap<String, Integer> intMap = new TreeMap<String, Integer>();
        
        // Put in some ints, automatically wrapped in Integer objects
        terminal.println(
          "Store 3 wrapped ints, keys \"one\", \"two\", \"three\"."); 
        intMap.put("one",   1 );
        intMap.put("two",   2 );
        intMap.put("three", 3 );

        // Get the value associated with a key
        Integer wrappedInt = intMap.get( "two" );
        
        // And print the wrapped int
        terminal.println( "The wrapped int stored for \"two\" is "
                          + wrappedInt);

	// The keySet method returns a Set<String> collection
        terminal.println( "\nIterate over keys, get each value." );
        terminal.println( "Note that key order is aphabetical:" );
	for ( String key : intMap.keySet() ) { 
            terminal.println( "The value for key " + key + " is "
                              + ( intMap.get( key)) );
	}

        // Iterate over the collection of values;
        // notice the order is the same (ie the key-order).
        terminal.println( "\nIterate over the values:" );
	for ( Integer value : intMap.values() ) {
            terminal.println( value );
	}

        // The set of Map.Entry objects (key-value pairs);
        // Map.Entry is an inner class of Map.

        // Iterate over the entries.
        terminal.println( "\nIterate over the key-value pairs:" );
	for ( Map.Entry<String, Integer> entry : intMap.entrySet() ) {
            terminal.println( "The value for the entry with key " 
                      + entry.getKey() + " is "
                      + entry.getValue());	 
   
	}

        // How a TreeMap represents itself  as a String:
        terminal.println(
            "\nHow a TreeMap represents itself as a String:");
        terminal.println(intMap.toString());       
        terminal.println();

        // We can overwrite the value stored under a key
        terminal.println(
            "Store a different value at key \"two\"");
        intMap.put("two", 2222 );
        terminal.println(intMap.toString());
        terminal.println();

        // We can store the same value under two keys
        terminal.println(
            "Store intMap.get( \"one\" ) at key \"two\"");
        intMap.put("two", intMap.get( "one" ) );
        terminal.println(intMap.toString());
        terminal.println();

        // And keys don't necessarily have to be Strings;
        // here's a TreeMap mapping Integers to Strings.

        TreeMap<Integer, String> map = new TreeMap<Integer, String>();

        terminal.println(
            "A TreeMap with Integer keys mapping to String values");
        map.put( 1, "I" );
        map.put( 2, "II" );
        map.put( 3, "III" );
        terminal.println(map.toString());
    }
}


