ObjectSpace Homepage

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

OrderedMap examples

OrderedMap1 - Construction, enumeration, access, rejection of duplicates.

OrderedMap2 - Accessing keys and values.

OrderedMap3 - Counting, finding, erasing.

OrderedMap4 - Bounds.

OrderedMap5 - Construction, enumeration, access, acceptance of duplicates.

OrderedMap6 - Accessing keys and values.

OrderedMap7 - Counting, finding, erasing.

OrderedMap8 - Bounds.

OrderedMap9 - Traverse a map with duplicate keys.


OrderedMap1 Example Code

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

/**
 * Construction, enumeration, access, rejection of duplicates.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap1
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap();
    map.add( new Integer( 2 ), "two" );
    map.add( new Integer( 4 ), "four" );
    System.out.println( map );
    System.out.println();

    System.out.println( "Enumerate the OrderedMap" );
    Enumeration e = map.elements();
    while ( e.hasMoreElements() )
      System.out.println( e.nextElement() );
    System.out.println();

    System.out.println( "Iterate through the OrderedMap" );
    for ( OrderedMapIterator i = map.begin(); !i.equals( map.end() ); i.advance() )
      System.out.println( i.get() + ", key = " + i.key() + ", value = " + i.value() );
    System.out.println();

    System.out.println( "Demonstrate access" );
    System.out.println( "map.at( 2 ) = " + map.get( new Integer( 2 ) ) );
    System.out.println( "map.at( 5 ) = " + map.get( new Integer( 5 ) ) );
    System.out.println( "map = " + map );
    System.out.println();

    System.out.println( "Show that duplicates cannot be added." );
    Object value = map.add( new Integer( 8 ), "eight" );
    if ( value != null )
      System.out.println( "Could not add 8." );
    else
      System.out.println( "Added 8." );
    System.out.println( "map = " + map );

    value = map.add( new Integer( 4 ), "FOUR" );
    if ( value != null )
      System.out.println( "Could not add 4." );
    else
      System.out.println( "Added 4." );
    System.out.println( "map = " + map );
    System.out.println();

    System.out.println( "Demonstrate modification" );
    map.put( new Integer( 4 ), "FOUR" );
    System.out.println( "map = " + map );
    }
  }

OrderedMap1 Example Output

OrderedMap( Pair( 2, two ), Pair( 4, four ) )

Enumerate the OrderedMap
two
four

Iterate through the OrderedMap
Pair( 2, two ), key = 2, value = two
Pair( 4, four ), key = 4, value = four

Demonstrate access
map.at( 2 ) = two
map.at( 5 ) = null
map = OrderedMap( Pair( 2, two ), Pair( 4, four ) )

Show that duplicates cannot be added.
Added 8.
map = OrderedMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) )
Could not add 4.
map = OrderedMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) )

Demonstrate modification
map = OrderedMap( Pair( 2, two ), Pair( 4, FOUR ), Pair( 8, eight ) )

OrderedMap2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
import java.util.Enumeration;

/**
 * Accessing keys and values.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap2
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap( new LessString() );
    map.add( "cat", "Meow" );
    map.add( "ape", "Squeak" );
    map.add( "dog", "Woof" );
    map.add( "bat", "Squeak" );
    System.out.println( map );
    System.out.println();

    System.out.println( "Enumerate the OrderedMap" );
    Enumeration e = map.elements();
    while ( e.hasMoreElements() )
      System.out.println( e.nextElement() );
    System.out.println();

    e = map.keys();
    System.out.print( "map.keys() = " );
    while ( e.hasMoreElements() )
      System.out.print( e.nextElement() + " ");
    System.out.println();

    e = map.keys( "Squeak" );
    System.out.print( "map.keys( Squeak ) = " );
    while ( e.hasMoreElements() )
      System.out.print( e.nextElement() + " ");
    System.out.println();

    e = map.values( "bat" );
    System.out.print( "map.values( bat ) = " );
    while ( e.hasMoreElements() )
      System.out.print( e.nextElement() + " ");
    System.out.println();
    }
  }

OrderedMap2 Example Output

OrderedMap( Pair( ape, Squeak ), Pair( bat, Squeak ), Pair( cat, Meow ), Pair( dog, Woof ) )

Enumerate the OrderedMap
Squeak
Squeak
Meow
Woof

map.keys() = ape bat cat dog
map.keys( Squeak ) = ape bat
map.values( bat ) = Squeak

OrderedMap3 Example Code

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

/**
 * Counting, finding, erasing.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap3
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap( new LessString() );
    map.add( "cat", "Meow" );
    map.add( "ape", "Squeak" );
    map.add( "dog", "Woof" );
    map.add( "bat", "Squeak" );
    System.out.println( map );
    System.out.println( "map.count( dog ) = " + map.count( "dog" ) );
    OrderedMapIterator i = map.find( "dog" );
    if ( i.equals( map.end() ) ) // A simpler way: if ( i.atEnd() ) ...
      System.out.println( "Could not find dog." );
    else
      System.out.println( "Found " + i.get() );
    System.out.println( "map.remove( dog ) = " + map.remove( "dog" ) );
    OrderedMapIterator j = map.find( "dog" );
    if ( j.atEnd() ) // A simpler way: if ( j.equals( map.end() ) ) ...
      System.out.println( "Could not find dog." );
    else
      System.out.println( "Found " + j.get() );
    }
  }

OrderedMap3 Example Output

OrderedMap( Pair( ape, Squeak ), Pair( bat, Squeak ), Pair( cat, Meow ), Pair( dog, Woof ) )
map.count( dog ) = 1
Found Pair( dog, Woof )
map.remove( dog ) = Woof
Could not find dog.

OrderedMap4 Example Code

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

/**
 * Bounds.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap4
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap();
    map.add( new Integer( 3 ), "three" );
    map.add( new Integer( 8 ), "eight" );
    map.add( new Integer( 2 ), "two" );
    map.add( new Integer( 10 ), "ten" );
    System.out.println( map );

    OrderedMapIterator lower = map.lowerBound( new Integer( 3 ) );
    System.out.println( "First pair whose key is not before 3 = " + lower.get() );

    OrderedMapIterator upper = map.upperBound( new Integer( 3 ) );
    System.out.println( "First pair whose key is after 3 = " + upper.get() );
    }
  }

OrderedMap4 Example Output

OrderedMap( Pair( 2, two ), Pair( 3, three ), Pair( 8, eight ), Pair( 10, ten ) )
First pair whose key is not before 3 = Pair( 3, three )
First pair whose key is after 3 = Pair( 8, eight )

OrderedMap5 Example Code

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

/**
 * Construction, enumeration, access, acceptance of duplicates.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap5
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap( true ); // allow duplicates
    map.add( new Integer( 2 ), "two" );
    map.add( new Integer( 4 ), "four" );
    System.out.println( map );
    System.out.println();

    System.out.println( "Enumerate the OrderedMap" );
    Enumeration e = map.elements();
    while ( e.hasMoreElements() )
      System.out.println( e.nextElement() );
    System.out.println();

    System.out.println( "Iterate through the OrderedMap" );
    for ( OrderedMapIterator i = map.begin(); !i.atEnd(); i.advance() )
      System.out.println( i.get() + ", key = " + i.key() + ", value = " + i.value() );
    System.out.println();

    System.out.println( "Show that duplicates can be added." );
    map.add( new Integer( 8 ), "eight" );
    System.out.println( "map = " + map );

    map.add( new Integer( 4 ), "FOUR" );
    System.out.println( "map = " + map );
    }
  }

OrderedMap5 Example Output

OrderedMap( Pair( 2, two ), Pair( 4, four ) )

Enumerate the OrderedMap
two
four

Iterate through the OrderedMap
Pair( 2, two ), key = 2, value = two
Pair( 4, four ), key = 4, value = four

Show that duplicates can be added.
map = OrderedMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) )
map = OrderedMap( Pair( 2, two ), Pair( 4, four ), Pair( 4, FOUR ), Pair( 8, eight ) )

OrderedMap6 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
import java.util.Enumeration;

/**
 * Accessing keys and values.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap6
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap( new LessString(), true );
    map.add( "cat", "Meow" );
    map.add( "ape", "Squeak" );
    map.add( "ape", "Whoop" );
    map.add( "bat", "Squeak" );
    System.out.println( "map = " + map );

    System.out.println( "Enumerate the OrderedMap" );
    Enumeration e = map.elements();
    while ( e.hasMoreElements() )
      System.out.println( e.nextElement() );
    System.out.println();

    e = map.keys();
    System.out.print( "map.keys() = " );
    while ( e.hasMoreElements() )
      System.out.print( e.nextElement() + " ");
    System.out.println();

    e = map.keys( "Squeak" );
    System.out.print( "map.keys( Squeak ) = " );
    while ( e.hasMoreElements() )
      System.out.print( e.nextElement() + " ");
    System.out.println();

    e = map.values( "ape" );
    System.out.print( "map.values( ape ) = " );
    while ( e.hasMoreElements() )
      System.out.print( e.nextElement() + " ");
    System.out.println();
    }
  }

OrderedMap6 Example Output

map = OrderedMap( Pair( ape, Squeak ), Pair( ape, Whoop ), Pair( bat, Squeak ), Pair( cat, Meow ) )
Enumerate the OrderedMap
Squeak
Whoop
Squeak
Meow

map.keys() = ape ape bat cat
map.keys( Squeak ) = ape bat
map.values( ape ) = Squeak Whoop

OrderedMap7 Example Code

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

/**
 * Counting, finding, erasing.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap7
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap( new LessString(), true );
    map.add( "cat", "Meow" );
    map.add( "ape", "Squeak" );
    map.add( "ape", "Whoop" );
    map.add( "bat", "Squeak" );
    System.out.println( map );

    System.out.println( "map.count( ape ) = " + map.count( "ape" ) );
    OrderedMapIterator i = map.find( "ape" );

    if ( i.equals( map.end() ) ) // A simpler way: if ( i.atEnd() ) ...
      {
      System.out.println( "Could not find dog." );
      }
    else
      {
      while ( i.key().equals( "ape" ) )
        {
        System.out.println( "Found " + i.get() );
        i.advance();
        }
      }
    System.out.println( "map.remove( ape ) = " + map.remove( "ape" ) );
    OrderedMapIterator j = map.find( "ape" );
    if ( j.atEnd() ) // A simpler way: if ( j.equals( map.end() ) ) ...
      System.out.println( "Could not find ape." );
    else
      System.out.println( "Found " + j.get() );
    }
  }

OrderedMap7 Example Output

OrderedMap( Pair( ape, Squeak ), Pair( ape, Whoop ), Pair( bat, Squeak ), Pair( cat, Meow ) )
map.count( ape ) = 2
Found Pair( ape, Squeak )
Found Pair( ape, Whoop )
map.remove( ape ) = Squeak
Could not find ape.

OrderedMap8 Example Code

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

/**
 * Bounds.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap8
  {
  public static void main( String[] args )
    {
    OrderedMap map = new OrderedMap( true ); // allow duplicates
    map.add( new Integer( 3 ), "three" );
    map.add( new Integer( 8 ), "eight" );
    map.add( new Integer( 2 ), "two" );
    map.add( new Integer( 10 ), "ten" );
    System.out.println( map );

    OrderedMapIterator lower = map.lowerBound( new Integer( 3 ) );
    System.out.println( "First pair whose key is not before 3 = " + lower.get() );

    OrderedMapIterator upper = map.upperBound( new Integer( 3 ) );
    System.out.println( "First pair whose key is after 3 = " + upper.get() );

    Range range = map.equalRange( new Integer( 3 ) );
    System.out.println( "first of equalRange = " + range.begin.get() );
    System.out.println( "second of equalRange = " + range.end.get() );

      System.out.println( "Iterating values in the range..." );
    ForwardIterator begin = range.begin;
    ForwardIterator end = range.end;
    while ( ! begin.equals( end ) )
      {
      System.out.println( begin.get() );
      begin.advance();
      }
    }
  }

OrderedMap8 Example Output

OrderedMap( Pair( 2, two ), Pair( 3, three ), Pair( 8, eight ), Pair( 10, ten ) )
First pair whose key is not before 3 = Pair( 3, three )
First pair whose key is after 3 = Pair( 8, eight )
first of equalRange = Pair( 3, three )
second of equalRange = Pair( 8, eight )
Iterating values in the range...
Pair( 3, three )

OrderedMap9 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;

/**
 * Traverse a map with duplicate keys.
 *
 * @see com.objectspace.jgl,OrderedMap
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class OrderedMap9
  {
  public static void main( String[] args )
    {
    // Create a map that allows duplicate keys.
    OrderedMap map = new OrderedMap( true );
    // Populate the map, being sure to use duplicate keys.
    map.add( new Integer( 86 ), "Texas Fight" );
    map.add( new Integer( 42 ), "Bevo" );
    map.add( new Integer( 69 ), "University of Texas" );
    map.add( new Integer( 42 ), "Hook 'Em" );
    map.add( new Integer( 7 ), "Disciplina Praesidium Civitatis" );

    System.out.println( "-----wrong" );
      {
      // Enumerate the wrong way.
      Enumeration keys = map.keys();
      while ( keys.hasMoreElements() )
        {
        // Get the key.
        Integer i = (Integer)keys.nextElement();
        // Try and find the value using the key.
        System.out.println( "Key=" + i + "\tValue=" + map.get( i ) );
        }
      }

    System.out.println( "-----easy" );
      {
      // Enumerate the easy way
      Enumeration pairs = map.start();
      while ( pairs.hasMoreElements() )
        {
        Pair p = (Pair)pairs.nextElement();
        // p is a key-value pair, so we have all the info we need.
        System.out.println( "Key=" + p.first + "\tValue=" + p.second );
        }
      }

    System.out.println( "-----hard" );
      {
      // Enumerate the hard way
      Enumeration keys = map.keys();
      while ( keys.hasMoreElements() )
        {
        // Get the key.
        Integer i = (Integer)keys.nextElement();
        // get the value(s) associated with the key pair.
        Range r = map.equalRange( i );
        // Loop until all values have been processed.
        while ( true )
          {
          // Notice equalRange() enumerations return a key-value pair.
          Pair p = (Pair)r.begin.nextElement();
          // p.first will always be the same as i
          System.out.println( "Key=" + p.first + "\tValue=" + p.second );
          // Are we done?
          if ( r.begin.equals( r.end ) )
            break;
          // We know the next key is the same as this one, so skip it.
          keys.nextElement();
          }
        }
      }
  }
}

OrderedMap9 Example Output

-----wrong
Key=7 Value=Disciplina Praesidium Civitatis
Key=42 Value=Hook 'Em
Key=42 Value=Hook 'Em
Key=69 Value=University of Texas
Key=86 Value=Texas Fight
-----easy
Key=7 Value=Disciplina Praesidium Civitatis
Key=42 Value=Bevo
Key=42 Value=Hook 'Em
Key=69 Value=University of Texas
Key=86 Value=Texas Fight
-----hard
Key=7 Value=Disciplina Praesidium Civitatis
Key=42 Value=Bevo
Key=42 Value=Hook 'Em
Key=69 Value=University of Texas
Key=86 Value=Texas Fight

All Packages  Class Hierarchy  This Package  Previous  Next  Index