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

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. import com.objectspace.jgl.*;
  3. import com.objectspace.jgl.algorithms.*;
  4.  
  5. /**
  6.  * Selecting and rejecting elements from a container
  7.  *
  8.  * @see com.objectspace.jgl.algorithms.Filtering
  9.  * @version 3.0.0
  10.  * @author ObjectSpace, Inc.
  11.  */
  12.  
  13. public class Filtering2
  14.   {
  15.   public static void main( String[] args )
  16.     {
  17.     HashSet set = new HashSet( true ); // allow duplicates
  18.     set.add( "dog" );
  19.     set.add( "monkey" );
  20.     set.add( "lion" );
  21.     set.add( "dog" );
  22.     set.add( "armadillo" );
  23.     set.add( "zebra" );
  24.     System.out.println( "Original set: " + set + "\n" );
  25.  
  26.     System.out.println
  27.       (
  28.       "Collecting strings > 5 chars: " + Filtering.select
  29.         (
  30.         set,
  31.         new Filtering2UnaryPredicate()
  32.         )
  33.       );
  34.     System.out.println
  35.       (
  36.       " Rejecting strings > 5 chars: " + Filtering.reject
  37.         (
  38.         set,
  39.         new Filtering2UnaryPredicate()
  40.         )
  41.       );
  42.     }
  43.   }
  44.  
  45. class Filtering2UnaryPredicate implements UnaryPredicate
  46.   {
  47.   // return true if the length of the toString() is
  48.   // greater than 5.
  49.   public boolean execute( Object object )
  50.     {
  51.     return object.toString().length() > 5;
  52.     }
  53.   }
  54.