ObjectSpace Homepage

JGL - The Generic Collection Library for Java
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Filtering examples

Filtering1 - Collapsing consecutive elements in a native array of Objects and an JGL container.

Filtering2 - Selecting and rejecting elements from a container


Filtering1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.util.*;

/**
 * Collapsing consecutive elements in a native array of Objects and an JGL container.
 *
 * @see com.objectspace.jgl.algorithms.Filtering
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Filtering1
  {
  public static void main( String[] args )
    {
    String[] strings = { "gnu", "emu", "emu", "fox", "fox", "fox", "gnu" };
    ObjectIterator begin = ObjectIterator.begin( strings );
    ObjectIterator end = ObjectIterator.end( strings );
    System.out.print( "strings = " );
    Printing.println( begin, end );
    ObjectIterator last = (ObjectIterator)Filtering.unique( begin, end );
    System.out.print( "filtered strings = " );
    Printing.println( begin, end );
    int remaining = begin.distance( last );
    System.out.println( "remaining = " + remaining );
    System.out.print( "filtered array with bounds given = " );
    end.retreat( remaining );
    Printing.println( begin, end );

    Array array = new Array();
    array.add( "gnu" );
    array.add( "emu" );
    array.add( "emu" );
    array.add( "fox" );
    array.add( "fox" );
    array.add( "fox" );
    array.add( "gnu" );
    System.out.println( "array = " + array );
    Deque deque = new Deque();
    Filtering.uniqueCopy( array, new InsertIterator( deque ) );
    System.out.println( "deque = " + deque );
    }
  }

Filtering1 Example Output

strings = ( gnu, emu, emu, fox, fox, fox, gnu )
filtered strings = ( gnu, emu, fox, gnu, fox, fox, gnu )
remaining = 4
filtered array with bounds given = ( gnu, emu, fox )
array = Array( gnu, emu, emu, fox, fox, fox, gnu )
deque = Deque( gnu, emu, fox, gnu )

Filtering2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;

/**
 * Selecting and rejecting elements from a container
 *
 * @see com.objectspace.jgl.algorithms.Filtering
 * @version 3.0.0
 * @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;
    }
  }

Filtering2 Example Output

Original set: HashSet( dog, dog, armadillo, monkey, zebra, lion )

Collecting strings > 5 chars: HashSet( armadillo, monkey )
 Rejecting strings > 5 chars: HashSet( dog, dog, zebra, lion )

All Packages  Class Hierarchy  This Package  Previous  Next  Index