HashMap 1 - Construction, enumeration, access, rejection of duplicates.
HashMap 2 - Accessing keys and values.
HashMap 3 - Counting, finding, erasing.
HashMap 4 - Construction, enumeration, access, acceptance of duplicates.
HashMap 5 - Accessing keys and values.
HashMap 6 - Counting, finding, erasing.
HashMap 7 - Bounds.
HashMap1 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import java.util.Enumeration; import jgl.*; /** * Construction, enumeration, access, rejection of duplicates. *
* @see jgl.HashMap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap1 { public static void main( String[] args ) { HashMap map = new HashMap(); map.add( new Integer( 2 ), "two" ); map.add( new Integer( 4 ), "four" ); System.out.println( map ); System.out.println(); System.out.println( "Enumerate the HashMap" ); Enumeration e = map.elements(); while( e.hasMoreElements() ) System.out.println( e.nextElement() ); System.out.println(); System.out.println( "Iterate through the HashMap" ); for( HashMapIterator i = map.begin(); !i.atEnd(); 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.get( 2 ) = " + map.get( new Integer( 2 ) ) ); System.out.println( "map.get( 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 ); } }
HashMap( Pair( 2, two ), Pair( 4, four ) ) Enumerate the HashMap two four Iterate through the HashMap Pair( 2, two ), key = 2, value = two Pair( 4, four ), key = 4, value = four Demonstrate access map.get( 2 ) = two map.get( 5 ) = null map = HashMap( Pair( 2, two ), Pair( 4, four ) ) Show that duplicates cannot be added. Added 8. map = HashMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) ) Could not add 4. map = HashMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) ) Demonstrate modification map = HashMap( Pair( 2, two ), Pair( 4, FOUR ), Pair( 8, eight ) ) HashMap1 Example Output
HashMap2 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import java.util.Enumeration; import jgl.*; /** * Accessing keys and values. *
* @see jgl.HashMap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap2 { public static void main( String[] args ) { HashMap map = new HashMap(); map.add( "cat", "Meow" ); map.add( "ape", "Squeak" ); map.add( "dog", "Woof" ); map.add( "bat", "Squeak" ); System.out.println( "map = " + map ); System.out.print( "Enumerate the HashMap: " ); Enumeration e = map.elements(); while( e.hasMoreElements() ) System.out.print( e.nextElement() + " "); System.out.println(); System.out.print( "map.keys() = " ); e = map.keys(); while( e.hasMoreElements() ) System.out.print( e.nextElement() + " "); System.out.println(); System.out.print( "map.keys( Squeak ) = " ); e = map.keys( "Squeak" ); while( e.hasMoreElements() ) System.out.print( e.nextElement() + " "); System.out.println(); System.out.print( "map.values( bat ) = " ); e = map.values( "bat" ); while( e.hasMoreElements() ) System.out.print( e.nextElement() + " "); System.out.println(); } }
map = HashMap( Pair( ape, Squeak ), Pair( dog, Woof ), Pair( bat, Squeak ), Pair( cat, Meow ) ) Enumerate the HashMap: Squeak Woof Squeak Meow map.keys() = ape dog bat cat map.keys( Squeak ) = ape bat map.values( bat ) = Squeak HashMap2 Example Output
HashMap3 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; /** * Counting, finding, erasing. *
* @see jgl.HashMap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap3 { public static void main( String[] args ) { HashMap map = new HashMap(); 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" ) ); HashMapIterator i = map.find( "dog" ); if( i.equals( map.end() ) ) // A simpler way of saying this is: 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" ) ); HashMapIterator j = map.find( "dog" ); if( j.atEnd() ) // A simpler way of saying: if( j.equals( map.end() ) ) ... System.out.println( "Could not find dog." ); else System.out.println( "Found " + j.get() ); } }
HashMap( Pair( ape, Squeak ), Pair( dog, Woof ), Pair( bat, Squeak ), Pair( cat, Meow ) ) map.count( dog ) = 1 Found Pair( dog, Woof ) map.remove( dog ) = Woof Could not find dog. HashMap3 Example Output
HashMap4 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import java.util.Enumeration; import jgl.*; /** * Construction, enumeration, access, acceptance of duplicates. *
* @see jgl.HashMap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap4 { public static void main( String[] args ) { HashMap map = new HashMap( true ); map.add( new Integer( 2 ), "two" ); map.add( new Integer( 4 ), "four" ); System.out.println( map ); System.out.println(); System.out.println( "Enumerate the HashMap" ); Enumeration e = map.elements(); while( e.hasMoreElements() ) System.out.println( e.nextElement() ); System.out.println(); System.out.println( "Iterate through the HashMap" ); for( HashMapIterator 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 ); System.out.println( "Show that even with duplicates, put() does a replacement." ); map.put( new Integer( 4 ), "FoUr" ); System.out.println( "map = " + map ); } }
HashMap( Pair( 2, two ), Pair( 4, four ) ) Enumerate the HashMap two four Iterate through the HashMap Pair( 2, two ), key = 2, value = two Pair( 4, four ), key = 4, value = four Show that duplicates can be added. map = HashMap( Pair( 2, two ), Pair( 4, four ), Pair( 8, eight ) ) map = HashMap( Pair( 2, two ), Pair( 4, four ), Pair( 4, FOUR ), Pair( 8, eight ) ) Show that even with duplicates, put() does a replacement. map = HashMap( Pair( 2, two ), Pair( 4, FoUr ), Pair( 4, FOUR ), Pair( 8, eight ) ) HashMap4 Example Output
HashMap5 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import java.util.Enumeration; import jgl.*; /** * Accessing keys and values. *
* @see jgl.HashMap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap5 { public static void main( String[] args ) { HashMap map = new HashMap( 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(); System.out.println( "Enumerate the HashMap" ); 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.keys( ape ) = " ); while( e.hasMoreElements() ) System.out.print( e.nextElement() + " " ); System.out.println(); } }
map = HashMap( Pair( ape, Squeak ), Pair( ape, Whoop ), Pair( bat, Squeak ), Pair( cat, Meow ) ) Enumerate the HashMap Squeak Whoop Squeak Meow map.keys() = ape ape bat cat map.keys( Squeak ) = ape bat map.keys( ape ) = Squeak Whoop HashMap5 Example Output
HashMap6 Example Code
import jgl.*; /** * Counting, finding, erasing. *
* @see jgl.HashMap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap6 { public static void main( String[] args ) { HashMap map = new HashMap( 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" ) ); HashMapIterator i = map.find( "ape" ); if( i.equals( map.end() ) ) // A simpler way of saying this is: if( i.atEnd() ) ... { System.out.println( "Could not find dog." ); } else { while( !i.atEnd() && i.key().equals( "ape" ) ) { System.out.println( "Found " + i.get() ); i.advance(); } } System.out.println( "map.remove( ape ) = " + map.remove( "ape" ) ); HashMapIterator j = map.find( "ape" ); if( j.atEnd() ) // A simpler way of saying: if( j.equals( map.end() ) ) ... System.out.println( "Could not find ape." ); else System.out.println( "Found " + j.get() ); } }
HashMap( 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. HashMap6 Example Output
HashMap7 Example Code
import jgl.*; /** * Bounds. *
* @see jgl.Hashmap * @version 1.1 * @author ObjectSpace, Inc. */ public class HashMap7 { public static void main( String[] args ) { HashMap map = new HashMap( true ); map.add( new Integer( 3 ), "three" ); map.add( new Integer( 8 ), "eight" ); map.add( new Integer( 2 ), "two" ); map.add( new Integer( 3 ), "THREE" ); System.out.println( map ); Range range = map.equalRange( new Integer( 3 ) ); while( !range.begin.equals( range.end ) ) System.out.println( "match @ " + range.begin.nextElement() ); } }
HashMap( Pair( 2, two ), Pair( 3, three ), Pair( 3, THREE ), Pair( 8, eight ) ) match @ Pair( 3, three ) match @ Pair( 3, THREE ) HashMap7 Example Output