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 )
{
final Map h = new HashMap( 149, 0.75f);
{
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 ) );
}
int inches = ( ( Integer ) h.get( "foot" ) ).intValue();
out.println( inches );
out.println( "enumerate all the keys in the HashMap" );
for ( Iterator iter = h.keySet().iterator(); iter.hasNext(); )
{
String key = ( String ) iter.next();
int value = ( ( Integer ) h.get( key ) ).intValue();
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();
out.println( value );
}
out.println( "enumerate all the key/value Entries in the HashMap" );
for ( Iterator iter = h.entrySet().iterator(); iter.hasNext(); )
{
Map.Entry entry = ( Map.Entry ) iter.next();
out.println( "as Entry: " + entry );
String key = ( String ) entry.getKey();
int value = ( ( Integer ) entry.getValue() ).intValue();
out.println( "separately: " + key + " " + value );
}
Set justKeys = h.keySet();
String[] keys =
( String[] ) justKeys.toArray( new String[ justKeys.size() ] );
Collection justValues = h.values();
Integer[] values =
( Integer[] ) justValues.toArray( new Integer[ justValues.size() ] );
Set justEntries = h.entrySet();
Map.Entry[] keyValuePairs =
( Map.Entry[] ) justEntries.toArray( new Map.Entry[ justEntries
.size() ] );
}
}