• 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

What data structure?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic