wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What data structure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What data structure?" Watch "What data structure?" New topic
Author

What data structure?

Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
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

Joined: Nov 18, 2008
Posts: 371
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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Rob Prime wrote: So replace "new Double(2)" with "Double.valueOf(2)".
Or just "2D". Because autoboxing will wrap it using the valueOf method.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What data structure?
 
Similar Threads
String array initialization with constant values
MultiDimenaional Array sort
Class and Object Problem
iterating with a scriptlet, can't get the values into js arr
Display an array in a swing type box