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 ObjectSpace, Inc. import jgl.*; public class Maps1 { public static void main( String[] args ) { // Create a HashMap that doesn't allow duplicates. HashMap map = new HashMap(); Object value; value = map.add( "Dog", "Barky" ); 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 ); } }
value from add = null value from add = null map = HashMap( Pair( Cat, Beauty ), Pair( Dog, Barky ) ) value from add = Beauty map = HashMap( Pair( Cat, Beauty ), Pair( Dog, Barky ) ) Cat name is Beauty Ape name is null value from put = Beauty map = HashMap( Pair( Cat, Agatha ), Pair( Dog, Barky ) ) Cat name is Agatha Maps1 Example Output
Maps2 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; public class Maps2 { public static void main( String[] args ) { // Create a HashMap that allows duplicates. HashMap map = new HashMap( true ); Object value; value = map.add( "Dog", "Barky" ); 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 ); } }
value from add = null value from add = null map = HashMap( Pair( Cat, Beauty ), Pair( Dog, Barky ) ) value from add = null map = HashMap( Pair( Cat, Beauty ), Pair( Cat, Agatha ), Pair( Dog, Barky ) ) Cat name is Beauty Ape name is null value from put = Beauty map = HashMap( Pair( Cat, Agatha ), Pair( Cat, Agatha ), Pair( Dog, Barky ) ) Cat name is Agatha Maps2 Example Output
Maps3 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import 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", "Barky" ) ); 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() ); } }
map = HashMap( Pair( Cat, Beauty ), Pair( Cat, Agatha ), Pair( Dog, Barky ) ) Enumerator through values... Beauty Agatha Barky Enumerate through keys... Cat Cat Dog Maps3 Example Output
Maps4 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import 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 GreaterInteger() ); map2.put( new Integer( 1 ), "one" ); map2.put( new Integer( 3 ), "three" ); map2.put( new Integer( 2 ), "two" ); System.out.println( "map2 = " + map2 ); } }
map1 = OrderedMap( Pair( 1, one ), Pair( 2, two ), Pair( 3, three ) ) map2 = OrderedMap( Pair( 3, three ), Pair( 2, two ), Pair( 1, one ) ) Maps4 Example Output
Maps5 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import 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 ); // Allow dups. 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 ); } }
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 ) ) Maps5 Example Output
Maps6 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import 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 ) ); } }
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 Maps6 Example Output
Maps7 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import 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 ); } }
map = HashMap( Pair( CAT, Agatha ), Pair( DOG, Misty ) ) Maps7 Example Output