HashMap4 Example Code
// Copyright(c) 1996 ObjectSpace, Inc.
import java.util.Enumeration;
import jgl.*;
/**
* Construction, enumeration, access, acceptance of duplicates.
*
* @see jgl.HashMap
* @version 1.1
* @author ObjectSpace, Inc.
*/
public class HashMap4
{
public static void main( String[] args )
{
HashMap map = new HashMap( true );
map.add( new Integer( 2 ), "two" );
map.add( new Integer( 4 ), "four" );
System.out.println( map );
System.out.println();
System.out.println( "Enumerate the HashMap" );
Enumeration e = map.elements();
while( e.hasMoreElements() )
System.out.println( e.nextElement() );
System.out.println();
System.out.println( "Iterate through the HashMap" );
for( HashMapIterator i = map.begin(); !i.atEnd(); i.advance() )
System.out.println( i.get() + ", key = " + i.key() + ", value = " + i.value() );
System.out.println();
System.out.println( "Show that duplicates can be added." );
map.add( new Integer( 8 ), "eight" );
System.out.println( "map = " + map );
map.add( new Integer( 4 ), "FOUR" );
System.out.println( "map = " + map );
System.out.println( "Show that even with duplicates, put() does a replacement." );
map.put( new Integer( 4 ), "FoUr" );
System.out.println( "map = " + map );
}
}
HashMap4 Example Output
HashMap( Pair( 2, two ), Pair( 4, four ) )
Enumerate the HashMap
two
four
Iterate through the HashMap
Pair( 2, two ), key = 2, value = two
Pair( 4, four ), key = 4, value = four
Show that duplicates can be added.
map = HashMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) )
map = HashMap( Pair( 2, two ), Pair( 4, four ), Pair( 4, FOUR ), Pair( 8, eight ) )
Show that even with duplicates, put() does a replacement.
map = HashMap( Pair( 2, two ), Pair( 4, FoUr ), Pair( 4, FOUR ), Pair( 8, eight ) )