File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What data structure?" Watch "What data structure?" New topic

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

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

Joined: May 03, 2008
Posts: 5115

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

Joined: Oct 27, 2005
Posts: 20276

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.

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:
subject: What data structure?
It's not a secret anymore!