package com.mindprod.example;

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

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

    /**
     * Sample code to TEST HashMap with automatic boxing. requires JDK 1.5+
     *
     * @param args not used
     */
    @SuppressWarnings( "unchecked" )
    public static void main( String[] args )
        {
        // create a new HashMap
        HashMap<String, Integer> h = new HashMap<String, Integer>( 149
                /* capacity */,
                0.75f
                /* loadfactor */ );

        // add some key/value pairs about English measure to the HashMap
        h.put( "inch", 1 );
        h.put( "foot", 12 );
        h.put( "yard", 36 );
        h.put( "mile", 3760 * 36 );

        // look up a key in the HashMap
        int inches = h.get( "foot" );

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

        System.out.println( "enumerate all the keys in the HashMap" );
        for ( String key : h.keySet() )
            {
            int value = h.get( key );

            // 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 ( int value : h.values() )
            {
            // 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" );
        for ( Map.Entry<String, Integer> entry : h.entrySet() )
            {
            // prints lines of the form foot=12
            // in effectively random order.
            System.out.println( "as Entry: " + entry );

            // this does not require an expensive get lookup to find the value.
            String key = entry.getKey();
            int value = entry.getValue();
            System.out.println( "separately: " + key + " " + value );

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

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

            // extract key/value pair entries into an array
            Set<Map.Entry<String, Integer>> justEntries = h.entrySet();
            // Infuriatingly, this generates an unchecked conversion warning message.
            Map.Entry<String, Integer>[] keyValuePairs =
                    justEntries.toArray( new Map.Entry[justEntries.size()] );
            // Type erasure won't let us say:
            // Map.Entry<String, Integer>[] keyValuePairs =
            // justEntries.toArray ( new
            //  Map.Entry<String,Integer>[justEntries.size()] );
            }
        }// end main
    }