A hash is an mathematical operation on a object that has an O(1). From this hash, a hashmap can get to a bucket, which if it is an array, also has an O(1). This means that given any object as a key, it can get to the pair value in an O(1).
A array list is based on an array which has an O(1).
But this is not the point. The point is... a hashmap can get to any value based on any key. For example, if I want the color of a car, I can use the "color" key to get a string that returns the color. Same for "engine", "tires", etc.
If I want to do this with an array list -- and keep an O(1) -- I have to assign 0 to color, 1 to engine, etc. Of course, I can use two arraylists, one to hold the key and the other to hold value, but searching it won't be O(1). I have to find "color" in the first array, to find the index, so I can find the color value.
HashMap1 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.HashMap
* @version 3.0.0
* @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 );
}
}
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
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Accessing keys and values.
*
* @see com.objectspace.jgl.HashMap
* @version 3.0.0
* @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();
}
}
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
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Counting, finding, erasing.
*
* @see com.objectspace.jgl.HashMap
* @version 3.0.0
* @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: 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
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Construction, enumeration, access, acceptance of duplicates.
*
* @see com.objectspace.jgl.HashMap
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
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
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Accessing keys and values.
*
* @see com.objectspace.jgl.HashMap
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class HashMap5
{
public static void main( String[] args )
{
HashMap map = new HashMap( true ); // allow duplicates
map.add( "cat", "Meow"