[阅读: 537] 2005-11-04 08:20:29
import java.util.*;
public class MapTest {
private static String names[] = { "a", "b", "e",
"d", "f", "a", "c", "d", "f", "f" };
// build a HashMap and output contents
public MapTest()
{
HashMap map = new HashMap();
Integer i;
for ( int count = 0; count < names.length; count++ ) {
i = ( Integer ) map.get(
new Character( names[ count ].charAt( 0 ) ) );
// if key is not in map then give it value one
// otherwise increment its value by 1
if ( i == null )
map.put(
new Character( names[ count ].charAt( 0 ) ),
new Integer( 1 ) );
else
map.put(
new Character( names[ count ].charAt( 0 ) ),
new Integer( i.intValue() + 1 ) );
}
printMap( map );
}
// output map contents
public void printMap( Map mapRef )
{
System.out.println( mapRef.toString() );
}
// execute application
public static void main( String args[] )
{
new MapTest();
}
} // end class MapTest