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
{
/**
* Sample code to TEST HashMap. requires JDK 1.5+
*
* @param args not used
*/
@SuppressWarnings( "unchecked" )
public static void main( String[] args )
{
HashMap<String, String> h = new HashMap<String, String>( 149
,
0.75f
);
h.put( "WA", "Washington" );
h.put( "NY", "New York" );
h.put( "RI", "Rhode Island" );
h.put( "BC", "British Columbia" );
String stateName = h.get( "NY" );
System.out.println( stateName );
System.out.println( "enumerate all the keys in the HashMap" );
for ( String key : h.keySet() )
{
String value = h.get( key );
System.out.println( key + " " + value );
}
System.out.println( "enumerate all the values in the HashMap" );
for ( String value : h.values() )
{
System.out.println( value );
}
System.out
.println( "enumerate all the key/value Entries in the HashMap" );
for ( Map.Entry<String, String> entry : h.entrySet() )
{
System.out.println( "as Entry: " + entry );
String key = entry.getKey();
String value = entry.getValue();
System.out.println( "separately: " + key + " " + value );
}
Set<String> justKeys = h.keySet();
String[] keys = justKeys.toArray( new String[justKeys.size()] );
Collection<String> justValues = h.values();
String[] values = justValues.toArray( new String[justValues.size()] );
Set<Map.Entry<String, String>> justEntries = h.entrySet();
Map.Entry<String, String>[] keyValuePairs =
justEntries.toArray( new Map.Entry[justEntries.size()] );
}
}