This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtables - Same key many values?  RSS feed

 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a couple questions I couldn't really discern. It appears that I can use a Hashtable and plug objects into it using the same key?

If so, I'm having a bit of a problem and could use some help.



When I test it it works how I expect it to, however it only lists one String . There are two objects that it should be picking up... I'm just unsure how to stuff both of their "names" into my whoIsHere variable. If the two objects are positioned at different places, everything displays as it should. The only time it seems to be overwritten is when they occupy the same position.

Any help would be appreciated. I'm afraid I'm overlooking something simple here and it's driving me nuts.

Nate
[ January 16, 2007: Message edited by: Nathan Leniz ]
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you put same key in a map, for different value, the old one will be replaced with the new one...

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ricky says, if you put something into the HashMap while there is already data present with the same key, then the value you put in will replace the existing data.

In a Map, the keys have to be unique. If you want to store multiple values under the same key, then you can use an array or a list as the value.

Here's some example code. (Note, it makes use of generics, a Java 5.0 feature).
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya very much true that the old element is knocked off and the new one is paced at that key position....

Note the following code::
////////////////
public class TestData
{
public static void main(String [] s)
{
Hashtable ht = new Hashtable();
ht.put("One","1");
System.out.println(ht.put("One","2"));
}
}
///////////

it prints 1 on running...so the replaced object is returned
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
If you want to store multiple values under the same key, then you can use an array or a list as the value.


Or you use a MultiMap, as provided by Jakarty Commons Collections: http://jakarta.apache.org/commons/collections/apidocs/org/apache/commons/collections/MultiMap.html
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, thanks everyone. What I absolutely need is for the String to be concatenated every time the same key is written to. I'll look at that MultiMap, thanks!
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it to work! I created a second enumeration and cycled through my hashtable comparing everything to the table again. The second pass through I did a check so that if something had the same proposed key as one present but also had a different name it pulled the String object, concatenated it, and stuck it back in the hashtable.

With that said... I'm sure there are better ways to do this to make it faster/smaller. Any ideas?

 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using Map<Integer,StringBuilder>?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!