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. Sample code to TEST HashMap. requires JDK 1.5+
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
@SuppressWarnings( { "UnusedAssignment" } )
public final class TestHashMap
    {
// --------------------------- main() method ---------------------------

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

        // add some key/value pairs to the HashMap
        h.put( "WA", "Washington" );
        h.put( "NY", "New York" );
        h.put( "RI", "Rhode Island" );
        h.put( "BC", "British Columbia" );

        // look up a key in the HashMap
        String stateName = h.get( "NY" );

        // prints "New York"
        System.out.println( stateName );

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

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

        System.out.println( "enumerate all the values in the HashMap" );
        for ( String value : h.values() )
            {
            // prints lines of the form New York
            // in effectively random order.
            System.out.println( value );
            }

        System.out
                .println( "enumerate all the key/value Entries in the HashMap" );
        for ( Map.Entry<String, String> entry : h.entrySet() )
            {
            // prints lines of the form NY=New York
            // 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();
            String 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 end 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<String> justValues = h.values();
        String[] values = justValues.toArray( new String[justValues.size()] );

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