• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashtables - Same key many values?

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using Map<Integer,StringBuilder>?

 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic