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
{
/**
* Sample code to TEST HashMap with automatic boxing. requires JDK 1.5+
*
* @param args not used
*/
@SuppressWarnings( "unchecked" )
public static void main( String[] args )
{
HashMap<String, Integer> h = new HashMap<String, Integer>( 149
,
0.75f
);
h.put( "inch", 1 );
h.put( "foot", 12 );
h.put( "yard", 36 );
h.put( "mile", 3760 * 36 );
int inches = h.get( "foot" );
System.out.println( inches );
System.out.println( "enumerate all the keys in the HashMap" );
for ( String key : h.keySet() )
{
int value = h.get( key );
System.out.println( key + " " + value );
}
System.out.println( "enumerate all the values in the HashMap" );
for ( int value : h.values() )
{
System.out.println( value );
}
System.out
.println( "enumerate all the key/value Entries in the HashMap" );
for ( Map.Entry<String, Integer> entry : h.entrySet() )
{
System.out.println( "as Entry: " + entry );
String key = entry.getKey();
int value = entry.getValue();
System.out.println( "separately: " + key + " " + value );
Set<String> justKeys = h.keySet();
String[] keys = justKeys.toArray( new String[justKeys.size()] );
Collection<Integer> justValues = h.values();
Integer[] values =
justValues.toArray( new Integer[justValues.size()] );
Set<Map.Entry<String, Integer>> justEntries = h.entrySet();
Map.Entry<String, Integer>[] keyValuePairs =
justEntries.toArray( new Map.Entry[justEntries.size()] );
}
}
}