home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-30 | 1.2 KB | 54 lines |
- // Copyright(c) 1996,1997 ObjectSpace, Inc.
-
- import COM.objectspace.jgl.*;
-
- /**
- * Selecting and rejecting elements from a container
- *
- * @see COM.objectspace.jgl.Filtering
- * @version 2.0.2
- * @author ObjectSpace, Inc.
- */
-
- public class Filtering2
- {
- public static void main( String[] args )
- {
- HashSet set = new HashSet( true ); // allow duplicates
- set.add( "dog" );
- set.add( "monkey" );
- set.add( "lion" );
- set.add( "dog" );
- set.add( "armadillo" );
- set.add( "zebra" );
- System.out.println( "Original set: " + set + "\n" );
-
- System.out.println
- (
- "Collecting strings > 5 chars: " + Filtering.select
- (
- set,
- new Filtering2UnaryPredicate()
- )
- );
- System.out.println
- (
- " Rejecting strings > 5 chars: " + Filtering.reject
- (
- set,
- new Filtering2UnaryPredicate()
- )
- );
- }
- }
-
- class Filtering2UnaryPredicate implements UnaryPredicate
- {
- // return true if the length of the toString() is
- // greater than 5.
- public boolean execute( Object object )
- {
- return object.toString().length() > 5;
- }
- }
-