All Packages  Class Hierarchy  Previous  Next  Index

Maps Examples

Maps 1 - No description available.

Maps 2 - No description available.

Maps 3 - No description available.

Maps 4 - No description available.

Maps 5 - No description available.

Maps 6 - No description available.

Maps 7 - No description available.


Maps1 Example Code

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

public class Maps1
  {
  public static void main( String[] args )
    {
    HashMap map = new HashMap();
    Object value;

    value = map.add( "Dog", "Marble" );
    System.out.println( "value from add = " + value );

    value = map.add( "Cat", "Beauty" );
    System.out.println( "value from add = " + value );
    System.out.println( "map = " + map );

    value = map.add( "Cat", "Agatha" );
    System.out.println( "value from add = " + value );
    System.out.println( "map = " + map );

    value = map.get( "Cat" );
    System.out.println( "Cat name is " + value );

    value = map.get( "Ape" );
    System.out.println( "Ape name is " + value );

    value = map.put( "Cat", "Agatha" );
    System.out.println( "value from put = " + value );
    System.out.println( "map = " + map );

    String name3 = (String)map.get( "Cat" );
    System.out.println( "Cat name is " + name3 );
    }
  }

Maps1 Example Output

value from add = null
value from add = null
map = HashMap( Pair( Cat, Beauty ), Pair( Dog, Marble ) )
value from add = Beauty
map = HashMap( Pair( Cat, Beauty ), Pair( Dog, Marble ) )
Cat name is Beauty
Ape name is null
value from put = Beauty
map = HashMap( Pair( Cat, Agatha ), Pair( Dog, Marble ) )
Cat name is Agatha

Maps2 Example Code

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

public class Maps2
  {
  public static void main( String[] args )
    {
    HashMap map = new HashMap( true ); // allow duplicates
    Object value;

    value = map.add( "Dog", "Marble" );
    System.out.println( "value from add = " + value );

    value = map.add( "Cat", "Beauty" );
    System.out.println( "value from add = " + value );
    System.out.println( "map = " + map );

    value = map.add( "Cat", "Agatha" );
    System.out.println( "value from add = " + value );
    System.out.println( "map = " + map );

    value = map.get( "Cat" );
    System.out.println( "Cat name is " + value );

    value = map.get( "Ape" );
    System.out.println( "Ape name is " + value );

    value = map.put( "Cat", "Agatha" );
    System.out.println( "value from put = " + value );
    System.out.println( "map = " + map );

    String name3 = (String) map.get( "Cat" );
    System.out.println( "Cat name is " + name3 );
    }
  }

Maps2 Example Output

value from add = null
value from add = null
map = HashMap( Pair( Cat, Beauty ), Pair( Dog, Marble ) )
value from add = null
map = HashMap( Pair( Cat, Beauty ), Pair( Cat, Agatha ), Pair( Dog, Marble ) )
Cat name is Beauty
Ape name is null
value from put = Beauty
map = HashMap( Pair( Cat, Agatha ), Pair( Cat, Agatha ), Pair( Dog, Marble ) )
Cat name is Agatha

Maps3 Example Code

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

public class Maps3
  {
  public static void main( String[] args )
    {
    HashMap map = new HashMap( true ); // allow duplicates
    map.add( new Pair( "Dog", "Marble" ) );
    map.add( new Pair( "Cat", "Beauty" ) );
    map.add( new Pair( "Cat", "Agatha" ) );
    System.out.println( "map = " + map );

    System.out.println( "Enumerator through values..." );
    Enumeration values = map.elements();
    while ( values.hasMoreElements() )
      System.out.println( "  " + values.nextElement() );

    System.out.println( "Enumerate through keys..." );
    Enumeration keys = map.keys();
    while ( keys.hasMoreElements() )
      System.out.println( "  " + keys.nextElement() );
    }
  }

Maps3 Example Output

map = HashMap( Pair( Cat, Beauty ), Pair( Cat, Agatha ), Pair( Dog, Marble ) )
Enumerator through values...
  Beauty
  Agatha
  Marble
Enumerate through keys...
  Cat
  Cat
  Dog

Maps4 Example Code

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

public class Maps4
  {
  public static void main( String[] args )
    {
    // Order key-value pairs based on the hash value of the key.
    OrderedMap map1 = new OrderedMap();
    map1.put( new Integer( 1 ), "one" );
    map1.put( new Integer( 3 ), "three" );
    map1.put( new Integer( 2 ), "two" );
    System.out.println( "map1 = " + map1 );

    // Order key-value pairs in descending numeric order of key.
    OrderedMap map2 = new OrderedMap( new GreaterNumber() );
    map2.put( new Integer( 1 ), "one" );
    map2.put( new Integer( 3 ), "three" );
    map2.put( new Integer( 2 ), "two" );
    System.out.println( "map2 = " + map2 );
    }
  }

Maps4 Example Output

map1 = OrderedMap( Pair( 1, one ), Pair( 2, two ), Pair( 3, three ) )
map2 = OrderedMap( Pair( 3, three ), Pair( 2, two ), Pair( 1, one ) )

Maps5 Example Code

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

public class Maps5
  {
  public static void main( String[] args )
    {
    // Order key-value pairs in descending lexicographical order of keys.
    OrderedMap map = new OrderedMap( new GreaterString(), true );
    map.add( "10", "X" );
    map.add( "10", "ten" );
    map.add( "5", "V" );
    map.add( "5", "five" );

    System.out.println( "map = " + map );
    int n = map.count( "10" );
    System.out.println( "There are " + n + " key-value pairs with key 10" );

    System.out.println( "Removing all occurrences of 10..." );
    map.remove( "10" );

    n = map.count( "10" );
    System.out.println( "There are now " + n + " key-value pairs with key 10" );
    System.out.println( "map = " + map );
    }
  }

Maps5 Example Output

map = OrderedMap( Pair( 5, V ), Pair( 5, five ), Pair( 10, X ), Pair( 10, ten ) )
There are 2 key-value pairs with key 10
Removing all occurrences of 10...
There are now 0 key-value pairs with key 10
map = OrderedMap( Pair( 5, V ), Pair( 5, five ) )

Maps6 Example Code

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

public class Maps6
  {
  public static void main( String[] args )
    {
    Integer i1 = new Integer( 2 );
    Integer i2 = new Integer( 2 );

    HashMap map1 = new HashMap();
    System.out.println( "Using equals() to compare elements..." );
    System.out.println( "map1.add( i1, two ) = " + map1.add( i1, "two" ) );
    System.out.println( "map1.add( i1, two ) = " + map1.add( i1, "two" ) );
    System.out.println( "map1.add( i2, TWO ) = " + map1.add( i2, "TWO" ) );
    System.out.println( "map1.get( i1 ) = " + map1.get( i1 ) );
    System.out.println( "map1.get( i2 ) = " + map1.get( i2 ) );

    HashMap map2 = new HashMap( new IdenticalTo() );
    System.out.println( "Using == to compare elements..." );
    System.out.println( "map2.add( i1, two ) = " + map2.add( i1, "two" ) );
    System.out.println( "map2.add( i1, two ) = " + map2.add( i1, "two" ) );
    System.out.println( "map2.add( i2, TWO ) = " + map2.add( i2, "TWO" ) );
    System.out.println( "map2.get( i1 ) = " + map2.get( i1 ) );
    System.out.println( "map2.get( i2 ) = " + map2.get( i2 ) );
    }
  }

Maps6 Example Output

Using equals() to compare elements...
map1.add( i1, two ) = null
map1.add( i1, two ) = two
map1.add( i2, TWO ) = two
map1.get( i1 ) = two
map1.get( i2 ) = two
Using == to compare elements...
map2.add( i1, two ) = null
map2.add( i1, two ) = two
map2.add( i2, TWO ) = null
map2.get( i1 ) = two
map2.get( i2 ) = TWO

Maps7 Example Code

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

public class Maps7
  {
  public static void main( String[] args )
    {
    Pair pair1 = new Pair( "CAT", "Agatha" );
    Pair pair2 = new Pair( "DOG", "Misty" );
    HashMap map = new HashMap();
    map.add( pair1 );
    map.add( pair2 );
    System.out.println( "map = " + map );
    }
  }

Maps7 Example Output

map = HashMap( Pair( CAT, Agatha ), Pair( DOG, Misty ) )

All Packages  Class Hierarchy  Previous  Next  Index