HashMap1 Example Code
import com.sfdc.sss.*;
import java.util.Enumeration;
/**
* Construction, enumeration, access, rejection of duplicates.
*/
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 );
}
}
HashMap1 Example Output
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 ) )
HashMap2 Example Code
//
import com.sfdc.sss.*;
import java.util.Enumeration;
/**
* Accessing keys and values.
*/
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();
}
}
HashMap2 Example Output
map = HashMap( Pair( dog, Woof ), Pair( ape, Squeak ), Pair( bat, Squeak ), Pair( cat, Meow ) )
Enumerate the HashMap: Woof Squeak Squeak Meow
map.keys() = dog ape bat cat
map.keys( Squeak ) = ape bat
map.values( bat ) = Squeak
HashMap3 Example Code
//
import com.sfdc.sss.*;
/**
* Counting, finding, erasing.
*/
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: 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: if ( j.equals( map.end() ) ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + j.get() );
}
}
HashMap3 Example Output
HashMap( Pair( dog, Woof ), Pair( ape, Squeak ), Pair( bat, Squeak ), Pair( cat, Meow ) )
map.count( dog ) = 1
Found Pair( dog, Woof )
map.remove( dog ) = Woof
Could not find dog.
HashMap4 Example Code
//
import com.sfdc.jgl.*;
import java.util.Enumeration;
/**
* Construction, enumeration, access, acceptance of duplicates.
*/
public class HashMap4
{
public static void main( String[] args )
{
HashMap map = new HashMap( 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 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 );
}
}
HashMap4 Example Output
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 ) )
HashMap5 Example Code
//
import com.sfdc.jgl.*;
import java.util.Enumeration;
/**
* Accessing keys and values.
*/
public class HashMap5
{
public static void main( String[] args )
{
HashMap map = new HashMap( true ); // allow duplicates
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();
}
}
HashMap5 Example Output
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
HashMap6 Example Code
//
import com.sfdc.jgl.*;
/**
* Counting, finding, erasing.
*/
public class HashMap6
{
public static void main( String[] args )
{
HashMap map = new HashMap( true ); // allow duplicates
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: 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: if ( j.equals( map.end() ) ) ...
System.out.println( "Could not find ape." );
else
System.out.println( "Found " + j.get() );
}
}
HashMap6 Example Output
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.
HashMap7 Example Code
//
import com.sfdc.jgl.*;
public class HashMap7
{
public static void main( String[] args )
{
HashMap map = new HashMap( 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( 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() );
}
}
HashMap7 Example Output
HashMap( Pair( 2, two ), Pair( 3, three ), Pair( 3, THREE ), Pair( 8, eight ) )
match @ Pair( 3, three )
match @ Pair( 3, THREE )