| Author |
about hashmap implementation of arraylist
|
Sekhar Chand
Ranch Hand
Joined: Apr 05, 2006
Posts: 73
|
|
Hi, when i faced interview , i was asked one question how to implement HashMap functionality by using the ArrayList. If any body knows the solution please let me know Thanks and regards Ratna Sekhar
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Not an advanced question. Moving...
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I think this means to write a class that works like Map but uses ArrayList internally. I was actually fool enough to do this with Vector my first week of Java because I hadn't thought to look in the API and find maps. What does a map have? Keys and values. How would you store a bunch of keys and a bunch of values to make these two methods work: public Object get( key ) public void put( key, value )
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Minh Tran
Greenhorn
Joined: Jun 03, 2007
Posts: 19
|
|
I guess you can. in ArrayList, index might be a key as in HashTable and using get(int index); will return the object
|
SCJP 1.4, SCWCD 1.4<br />email:mobject@gmail.com
|
 |
Sekhar Chand
Ranch Hand
Joined: Apr 05, 2006
Posts: 73
|
|
Yeah you can use index in List as an Key in hashmap. But if they give any object, so we have to store that one also. Here i got an idea , when ever you are writting your own put method, you have to use two array lists, in which one takes key and another takes value objects. So, the index of the one key and value pair will be the same. In get method , you will get a key, by taking that keys list and by iterating you can get the index of that key and by using the same index you can get the object from the another list(because the indexes of the key and value are same). So you can return that value from your get method. But instead of this logic there is something, means using one or two method we can implement the Hasmap by using the list. That i want, if any body knows please let me know Thanks and regards Ratna sekhar
|
 |
Jeremy Botha
Ranch Hand
Joined: Feb 16, 2005
Posts: 125
|
|
personally, I think the easiest (and most inefficient way) to do this is to use the hashcode of the key as the index to the value in the arrayList. that is, something like this: J
|
McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
|
 |
Devesh H Rao
Ranch Hand
Joined: Feb 09, 2002
Posts: 687
|
|
|
|
 |
Devesh H Rao
Ranch Hand
Joined: Feb 09, 2002
Posts: 687
|
|
Originally posted by Jeremy Botha: personally, I think the easiest (and most inefficient way) to do this is to use the hashcode of the key as the index to the value in the arrayList. that is, something like this: J
ArrayList al = new ArrayList(SOME_VERY_BIG_NUMBER); public void put(Object key, Object value) { al.insert(key.hashCode(), value); } It actually translates to a veryyyyyyyyyyyyyyy big number.... A random object creation threw up these numbers 17523401 8567361 9584176 19972507 32942009 8222510 18581223 3526198 7699183 14285251 [ June 14, 2007: Message edited by: Devesh H Rao ]
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
|
The solutions would only work till the time we have a unique key. Uniqueness of the key should also be checked.
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
Infact Jeremy's solution would solve the uniqueness problem. The size of the arrayList should be 2*Integer.MAX_VALUE. Which isn't possible. [ June 14, 2007: Message edited by: Anupam Sinha ]
|
 |
Minh Tran
Greenhorn
Joined: Jun 03, 2007
Posts: 19
|
|
Originally posted by Sekhar Chand: But instead of this logic there is something, means using one or two method we can implement the Hasmap by using the list.
Impossible to use it as standard JDK, please have a look at Java Collection Framework You can inherit them for your own Collection
|
 |
Jay Dilla
Ranch Hand
Joined: Aug 12, 2004
Posts: 198
|
|
|
Use a multi dimensional array
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
you have to use two array lists, in which one takes key and another takes value objects
Yeah, that's what I did before I found the Map interface. Then ... well, we don't have a smack self on forehead smiley. Of course we have to ask if the original question of implementing hashmap required using hashes. This two-list approach would be fine if he said replace HashMap or implement Map, but there's no hash in sight.
|
 |
 |
|
|
subject: about hashmap implementation of arraylist
|
|
|