Question:
Java ( hashmap and arraylist) help plz ???
fra
2008-04-03 01:48:15 UTC
some more infomations and exercises, pin eccc...for understund those arguments
thank you !!! :-)
Three answers:
2008-04-03 02:21:58 UTC
I want to thank funmansk for the example code. I will make a copy for later study. I want to add:



HashMap and ArrayList are part of the Collections framework. It is the Itinerator that is doing all the magic. HashMap has a performance penalty because it sorts as it stores, whereas ArrayList does not sort as it accumulates data. Getting stuff back from a Collections Object in the order they were created might not or might be important to your application -- the concept of Sychronized. Vector and ArrayList are almost the same thing, the Itinerator references by Oridinal 0,1,2,3,4. Vector is Sychronized, ArrayList is not.



for(Element e : myArrayList) // will walk the total Collection, but in the computer's arrangement



Same statement for Vector remembered which object was position 0, position 1...



And, thus the difference between Hashtable and HashMap... map can be commanded to order the list. Hashtable and map reference by a key or "word" -- really an Object.



Note: I specify Vector and Hashtable, these are class that have the same names as the old Vector and old Hashtable, but the Generics version, the makes Collections.



Just thought I'd mention that, the Collection gives the same commands for all of those class. The features we want react to the Itinerator.
funmansk
2008-04-03 01:53:59 UTC
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"
?
2016-10-17 01:35:04 UTC
place submit insert (first in first out meaning whenever you insert something each thing else gets pushed back one and the cost you have been putting is the 1st element interior the record 88.7 a hundred.0 ninety.5 ninety seven.a million fifty seven.9 a million- 88.7 // gets the cost at place 0 2- a hundred.0// gets the cost at place a million 3- ninety.5// gets the cost at place 2 4- 5 // what number aspects are interior the record 5- 88.7 // gets rid of the ingredient at spot 0 6- a hundred.0 ninety.5 ninety seven.a million fifty seven.9 //gets the values from front to back 7- ninety.5 //strikes the ingredient at spot a million.. yet of the hot record (#6) 8- a hundred.0 ninety seven.a million fifty seven.9 //gets the values from front to back (sorry had it backwards.. fastened now)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...