home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / examples / hashmap4.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  1.3 KB  |  49 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2.  
  3. import java.util.Enumeration;
  4. import jgl.*;
  5.  
  6. /**
  7.  * Construction, enumeration, access, acceptance of duplicates. 
  8.  * <p>
  9.  * @see jgl.HashMap
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public class HashMap4
  15.   {
  16.   public static void main( String[] args )
  17.     {
  18.     HashMap map = new HashMap( true );
  19.     map.add( new Integer( 2 ), "two" );
  20.     map.add( new Integer( 4 ), "four" );
  21.     System.out.println( map );
  22.     System.out.println();
  23.  
  24.     System.out.println( "Enumerate the HashMap" );    
  25.     Enumeration e = map.elements();
  26.     while( e.hasMoreElements() )
  27.       System.out.println( e.nextElement() );
  28.     System.out.println();
  29.  
  30.     System.out.println( "Iterate through the HashMap" );
  31.     for( HashMapIterator i = map.begin(); !i.atEnd(); i.advance() )
  32.       System.out.println( i.get() + ", key = " + i.key() + ", value = " + i.value() );
  33.     System.out.println();
  34.  
  35.     System.out.println( "Show that duplicates can be added." );
  36.     map.add( new Integer( 8 ), "eight" );
  37.     System.out.println( "map = " + map );
  38.  
  39.     map.add( new Integer( 4 ), "FOUR" );
  40.     System.out.println( "map = " + map );
  41.  
  42.     System.out.println( "Show that even with duplicates, put() does a replacement." );
  43.     map.put( new Integer( 4 ), "FoUr" );
  44.     System.out.println( "map = " + map );
  45.  
  46.     }
  47.   }
  48.  
  49.