home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / PriorityQueue1.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  1.5 KB  |  47 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2.  
  3. import java.util.Enumeration;
  4. import COM.objectspace.jgl.*;
  5.  
  6. /**
  7.  * Construction, enumeration, pushing, popping.
  8.  *
  9.  * @see COM.objectspace.jgl.PriorityQueue
  10.  * @version 2.0.2
  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 an
  19.     // Integer is its int value, this will order Integers in descending order.
  20.     PriorityQueue queue = new PriorityQueue();
  21.     queue.push( new Integer( 5 ) );
  22.     queue.push( new Integer( -2 ) );
  23.     queue.push( new Integer( 10 ) );
  24.     queue.push( new Integer( 6 ) );
  25.     queue.push( new Integer( 20 ) );
  26.     queue.push( new Integer( -10 ) );
  27.  
  28.     // Note that although a PriorityQueue always pops objects in the order determined
  29.     // by the comparator, this *does not* mean that the objects are stored in order
  30.     // within the internal data structure. The internal data structure is organized as
  31.     // a heap.
  32.     System.out.println( "Print the PriorityQueue." );
  33.     System.out.println( queue );
  34.     System.out.println();
  35.  
  36.     System.out.println( "Non-destructively enumerate the PriorityQueue." );
  37.     Enumeration e = queue.elements();
  38.     while ( e.hasMoreElements() )
  39.       System.out.println( e.nextElement() );
  40.     System.out.println();
  41.  
  42.     System.out.println( "Pop and print each element." );
  43.     while ( !queue.isEmpty() )
  44.       System.out.println( queue.pop() );
  45.     }
  46.   }
  47.