package com.mindprod.example;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * example use of java.util.HashMap manual boxing. Sample code to TEST HashMap with manual boxing. requires JDK 1.1+
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
@SuppressWarnings( { "UnusedAssignment" } )
public final class TestHashMapManualBoxing
    {
// --------------------------- main() method ---------------------------

    /**
     * Sample code to TEST HashMap with manual boxing. requires JDK 1.1+
     *
     * @param args not used <br> Turn off warnings about failing to use generics for the whole class, JDK 1.5+ only
     */
    @SuppressWarnings( { "unchecked", "ForLoopReplaceableByForEach", "UnnecessaryBoxing", "UnnecessaryUnboxing" } )
    public static void main( String[] args )
        {
        // create a new HashMap
        HashMap h = new HashMap( 149/* capacity */, 0.75f/* loadfactor */ );

        // add some key/value pairs about English measure to the HashMap
        // don't remove boxiing. This is demo of manual boxing.
        h.put( "inch", new Integer( 1 ) );
        h.put( "foot", new Integer( 12 ) );
        h.put( "yard", new Integer( 36 ) );
        h.put( "mile", new Integer( 3760 * 36 ) );

        // look up a key in the HashMap, and convert item back to int
        // don't do autoboxing. This a demo of manual boxing.
        int inches = ( ( Integer ) h.get( "foot" ) ).intValue();

        // prints "12"
        System.out.println( inches );

        System.out.println( "enumerate all the keys in the HashMap" );
        // use JDK 1.1+ style Iterator loop

        // Don't convert to for:each
        for ( Iterator iter = h.keySet().iterator(); iter.hasNext(); )
            {
            String key = ( String ) iter.next();
            int value = ( ( Integer ) h.get( key ) ).intValue();

            // prints lines of the form foot 12
            // in effectively random order
            System.out.println( key + " " + value );
            }

        System.out.println( "enumerate all the values in the HashMap" );
        for ( Iterator iter = h.values().iterator(); iter.hasNext(); )
            {
            int value = ( ( Integer ) iter.next() ).intValue();

            // prints lines of the form "12"
            // in effectively random order.
            System.out.println( value );
            }

        System.out
                .println( "enumerate all the key/value Entries in the HashMap" );

        // Don't convert to for:each
        for ( Iterator iter = h.entrySet().iterator(); iter.hasNext(); )
            {
            // prints lines of the form foot=12
            // in effectively random order.
            Map.Entry entry = ( Map.Entry ) iter.next();
            System.out.println( "as Entry: " + entry );

            // this does not require an expensive get lookup to find the value.
            String key = ( String ) entry.getKey();
            int value = ( ( Integer ) entry.getValue() ).intValue();

            System.out.println( "separately: " + key + " " + value );
            }// end for

        // extract the keys into an array
        Set justKeys = h.keySet();
        // Use toArray that takes an empty array,
        // otherwise we end up with a useless Object[] instead of a String[].
        String[] keys =
                ( String[] ) justKeys.toArray( new String[justKeys.size()] );

        // extract values into an array, may contain duplicates unlike a Set.
        Collection justValues = h.values();
        Integer[] values =
                ( Integer[] ) justValues.toArray( new Integer[justValues.size()] );

        // extract key/value pair entries into an array
        Set justEntries = h.entrySet();
        Map.Entry[] keyValuePairs =
                ( Map.Entry[] ) justEntries.toArray( new Map.Entry[justEntries
                        .size()] );
        }// end main
    }