home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / OrderedMap9.java < prev    next >
Text File  |  1998-05-08  |  2KB  |  78 lines

  1. // Copyright(c) 1997 ObjectSpace, Inc.
  2. import com.objectspace.jgl.*;
  3. import java.util.Enumeration;
  4.  
  5. /**
  6.  * Traverse a map with duplicate keys.
  7.  *
  8.  * @see com.objectspace.jgl,OrderedMap
  9.  * @version 3.0.0
  10.  * @author ObjectSpace, Inc.
  11.  */
  12.  
  13. public class OrderedMap9
  14.   {
  15.   public static void main( String[] args )
  16.     {
  17.     // Create a map that allows duplicate keys.
  18.     OrderedMap map = new OrderedMap( true );
  19.     // Populate the map, being sure to use duplicate keys.
  20.     map.add( new Integer( 86 ), "Texas Fight" );
  21.     map.add( new Integer( 42 ), "Bevo" );
  22.     map.add( new Integer( 69 ), "University of Texas" );
  23.     map.add( new Integer( 42 ), "Hook 'Em" );
  24.     map.add( new Integer( 7 ), "Disciplina Praesidium Civitatis" );
  25.  
  26.     System.out.println( "-----wrong" );
  27.       {
  28.       // Enumerate the wrong way.
  29.       Enumeration keys = map.keys();
  30.       while ( keys.hasMoreElements() )
  31.         {
  32.         // Get the key.
  33.         Integer i = (Integer)keys.nextElement();
  34.         // Try and find the value using the key.
  35.         System.out.println( "Key=" + i + "\tValue=" + map.get( i ) );
  36.         }
  37.       }
  38.  
  39.     System.out.println( "-----easy" );
  40.       {
  41.       // Enumerate the easy way
  42.       Enumeration pairs = map.start();
  43.       while ( pairs.hasMoreElements() )
  44.         {
  45.         Pair p = (Pair)pairs.nextElement();
  46.         // p is a key-value pair, so we have all the info we need.
  47.         System.out.println( "Key=" + p.first + "\tValue=" + p.second );
  48.         }
  49.       }
  50.  
  51.     System.out.println( "-----hard" );
  52.       {
  53.       // Enumerate the hard way
  54.       Enumeration keys = map.keys();
  55.       while ( keys.hasMoreElements() )
  56.         {
  57.         // Get the key.
  58.         Integer i = (Integer)keys.nextElement();
  59.         // get the value(s) associated with the key pair.
  60.         Range r = map.equalRange( i );
  61.         // Loop until all values have been processed.
  62.         while ( true )
  63.           {
  64.           // Notice equalRange() enumerations return a key-value pair.
  65.           Pair p = (Pair)r.begin.nextElement();
  66.           // p.first will always be the same as i
  67.           System.out.println( "Key=" + p.first + "\tValue=" + p.second );
  68.           // Are we done?
  69.           if ( r.begin.equals( r.end ) )
  70.             break;
  71.           // We know the next key is the same as this one, so skip it.
  72.           keys.nextElement();
  73.           }
  74.         }
  75.       }
  76.   }
  77. }
  78.