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

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. import com.objectspace.jgl.*;
  3. import com.objectspace.jgl.algorithms.*;
  4. import java.util.Enumeration;
  5.  
  6. /**
  7.  * Construction, enumeration, pushing, popping.
  8.  *
  9.  * @see com.objectspace.jgl.PriorityQueue
  10.  * @version 3.0.0
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public class PriorityQueue1
  15.   {
  16.   public static void main( String[] args )
  17.     {
  18.     // Use a HashComparator for comparing elements. Since the hash value of 
  19.     // an Integer is its int value, this will order Integers in descending 
  20.     // order.
  21.     PriorityQueue queue = new PriorityQueue();
  22.     queue.push( new Integer( 5 ) );
  23.     queue.push( new Integer( -2 ) );
  24.     queue.push( new Integer( 10 ) );
  25.     queue.push( new Integer( 6 ) );
  26.     queue.push( new Integer( 20 ) );
  27.     queue.push( new Integer( -10 ) );
  28.  
  29.     // Note that although a PriorityQueue always pops objects in the order 
  30.     // determined by the comparator, this *does not* mean that the objects 
  31.     // are stored in order within the internal data structure. The internal 
  32.     // data structure is organized as a heap.
  33.     System.out.println( "Print the PriorityQueue." );
  34.     System.out.println( queue );
  35.     System.out.println();
  36.  
  37.     System.out.println( "Non-destructively enumerate the PriorityQueue." );
  38.     Enumeration e = queue.elements();
  39.     while ( e.hasMoreElements() )
  40.       System.out.println( e.nextElement() );
  41.     System.out.println();
  42.  
  43.     System.out.println( "Pop and print each element." );
  44.     while ( !queue.isEmpty() )
  45.       System.out.println( queue.pop() );
  46.     }
  47.   }
  48.