home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / HashMap1.java < prev    next >
Text File  |  1997-07-30  |  2KB  |  62 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2.  
  3. import java.util.Enumeration;
  4. import COM.objectspace.jgl.*;
  5.  
  6. /**
  7.  * Construction, enumeration, access, rejection of duplicates.
  8.  *
  9.  * @see COM.objectspace.jgl.HashMap
  10.  * @version 2.0.2
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public class HashMap1
  15.   {
  16.   public static void main( String[] args )
  17.     {
  18.     HashMap map = new HashMap();
  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( "Demonstrate access" );
  36.     System.out.println( "map.get( 2 ) = " + map.get( new Integer( 2 ) ) );
  37.     System.out.println( "map.get( 5 ) = " + map.get( new Integer( 5 ) ) );
  38.     System.out.println( "map = " + map );
  39.     System.out.println();
  40.  
  41.     System.out.println( "Show that duplicates cannot be added." );
  42.     Object value = map.add( new Integer( 8 ), "eight" );
  43.     if ( value != null )
  44.       System.out.println( "Could not add 8." );
  45.     else
  46.       System.out.println( "Added 8." );
  47.     System.out.println( "map = " + map );
  48.  
  49.     value = map.add( new Integer( 4 ), "FOUR" );
  50.     if ( value != null )
  51.       System.out.println( "Could not add 4." );
  52.     else
  53.       System.out.println( "Added 4." );
  54.     System.out.println( "map = " + map );
  55.     System.out.println();
  56.  
  57.     System.out.println( "Demonstrate modification" );
  58.     map.put( new Integer( 4 ), "FOUR" );
  59.     System.out.println( "map = " + map );
  60.     }
  61.   }
  62.