/*
 * [TestHashMapManualBoxing.java]
 *
 * Summary: example use of java.util.HashMap manual boxing. Sample code to TEST HashMap with manual boxing.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

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

import static java.lang.System.*;

/**
 * example use of java.util.HashMap manual boxing. Sample code to TEST HashMap with manual boxing.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
@SuppressWarnings( { "UnusedAssignment" } )
public final class TestHashMapManualBoxing
    {
    /**
     * Sample code to TEST HashMap with manual boxing.
     *
     * @param args not used
     */
    @SuppressWarnings( { "unchecked", "ForLoopReplaceableByForEach", "UnnecessaryBoxing", "UnnecessaryUnboxing" } )
    public static void main( String[] args )
        {
        // create a new HashMap
        final Map 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"
        out.println( inches );
        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
            out.println( key + " " + value );
            }
        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.
            out.println( value );
            }
        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();
            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();
            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
    }