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

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2.  
  3. import java.util.Enumeration;
  4. import COM.objectspace.jgl.*;
  5.  
  6. /**
  7.  * Construction, enumeration, rejection of duplicates.
  8.  *
  9.  * @see COM.objectspace.jgl.HashSet
  10.  * @version 2.0.2
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public class HashSet1
  15.   {
  16.   public static void main( String[] args )
  17.     {
  18.     HashSet set = new HashSet();
  19.     set.add( new Integer( 6 ) );
  20.     set.add( new Integer( 1 ) );
  21.     set.add( new Integer( 4 ) );
  22.     System.out.println( set );
  23.     System.out.println();
  24.  
  25.     System.out.println( "Enumerate the HashSet" );
  26.     Enumeration e = set.elements();
  27.     while ( e.hasMoreElements() )
  28.       System.out.println( e.nextElement() );
  29.     System.out.println();
  30.  
  31.     System.out.println( "Iterate through the HashSet" );
  32.     for ( HashSetIterator i = set.begin(); !i.atEnd(); i.advance() )
  33.       System.out.println( i.get() );
  34.     System.out.println();
  35.  
  36.     System.out.println( "Show that duplicates cannot be added." );
  37.     Object value = set.add( new Integer( 8 ) );
  38.     if ( value != null )
  39.       System.out.println( "Could not add 8." );
  40.     else
  41.       {
  42.       System.out.println( "Added 8" );
  43.       System.out.println( "New contents are " + set );
  44.       }
  45.  
  46.     value = set.add( new Integer( 4 ) );
  47.     if ( value != null )
  48.       System.out.println( "Could not add 4." );
  49.     else
  50.       {
  51.       System.out.println( "Added 4." );
  52.       System.out.println( "New contents are " + set );
  53.       }
  54.     }
  55.   }
  56.