Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What data structure?

 
Steve De Costa
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is a beginner question or not but I'm guessing it will be a very basic one for any CS Grad.

I need a data structure that maps every one integer to a String and a Double;

for example

Key ---> [String, Double]
=== === ====
1 -------> [ "AB" , new Double(2)]
2 -------> [ "BC" , new Double(3)]


I thought of HashMap but I don't think it will work. Can anyone please recommend what kind of structure should I be using to store something like this?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HashMap allows the mapping of one key to one value. You need one key and two values. Hm... the values are related somehow. Maybe you just could join them together to form some new object type? The class of this type could be the value type for your HashMap.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke's suggestion is the best solution for your question as it was asked, but perhaps you would like to elaborate on the real-world situation that you are modeling here. Specifically, if each String "AB" "CD" etc. is only ever associated with one and the same Double value, you could use two maps: one of which maps the Integer to the String and the second maps the String to the Double.

If however each String may be associated with differing Double values you would create a class with two fields as suggested by Hauke, but in that case I would have to wonder why you would want to use the Double wrapper class and not a double primitive.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, you should stop using "new" when creating instances of primitive wrapper classes. These days all these classes have a valueOf method that will wrap a primitive in a matching wrapper instance. The advantage of using this method is that if there's some form of caching available it will use that cache, and possibly return an existing object instead of creating a new one each time. So replace "new Double(2)" with "Double.valueOf(2)".

Currently, Float and Double are the only classes without a cache. Boolean has a "cache" for both its values in Boolean.TRUE and Boolean.FALSE. Byte, Short and Long have a cache for values from -128 to 127. Integer has the same range by default but the upper bound can be modified using a system property. Character has a cache for values from 0 to 127.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: So replace "new Double(2)" with "Double.valueOf(2)".
Or just "2D". Because autoboxing will wrap it using the valueOf method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic