#How to access the value of hashmap when the value itself is a nested hashmap?

1 messages · Page 1 of 1 (latest)

vast pine
#

HashMap<String, HashMap<Integer, Double>> map = new HashMap<>();

If i use the get method, it would return the whole nested HashMap<Integer, Double>
How would I access the <Integer> value?

blissful wolfBOT
#

<@&987246399047479336> please have a look, thanks.

blissful wolfBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

velvet bison
#

I think you would need to call get on the result as well:java HashMap<String, HashMap<Integer, Double>> map = new HashMap<>(); double x = map.get("SomeMap").get(5);

The Integer value is used as a key however, for the inner hash maps, so if you have an Integer already you can get a Double using it, but to actually get an Integer value in an inner map, you would need to iterate over the inner map's keys.

vast pine
#

hm

velvet bison
#

If you're looking to store two values under each String key by chance, then typically instead of having a nested map, people would have a Map that has Pair objects as values, or something similar:```java
class Pair<F, S> {
F firstValue;
S secondValue;
}

Map<String, Pair<Integer, Double>> someMap = new HashMap<>();
someMap.put("map1", new Pair<>());
someMap.put("map2", new Pair<>());

Pair<Integer, Double> p1 = someMap.get("map1");
p1.firstValue = 4;
p1.secondValue = 10.392;```

blissful wolfBOT
velvet bison
#

If any of that is confusing, lmk. I can try to explain it in some other way.

vast pine
#

@velvet bison wait

velvet bison
#

What's up?

vast pine
#

i just googled and turn out you need to declare the hashmap as map. Then you can just use map.get(Stringvalue).get(map.get(Stringvalue)) no?

velvet bison
#

I don't think that will work. Also, HashMap is a subtype of Map, so any variable you declare as a HashMap you can call the same methods as Map on.

#

You're trying to access an Integer value in the nested map, right? Not a Double value?

vast pine
#

yes, but then evetually the double value too from the integer value

velvet bison
# vast pine yes, but then evetually the double value too from the integer value

Ohh. I would keep two separate maps then. Do you think that could work with your code?```java
Map<String, Integer> ints = new HashMap<>();
Map<Integer, Double> dbls = new HashMap<>();

// Later in your code:
int x = ints.get("some key");
// Do stuff with your integer:
System.out.println("The int: " + x + "is at key "some key"");

// Get the double associated with the integer:
double y = dbls.get(x);
System.out.println("The double: " + y + "is at key " + x + " in the double map");

blissful wolfBOT
velvet bison
#

Sorry, I had to go for a moment.

#

A Map<String, Map<Integer, Double>>, is sort of like a "two-key" map: to get a double value, you have to know the String and the Integer in advance. (You could technically iterate over the map(s) I suppose if you want to use that anyway.)

Having a Map<String, Integer> and another Map<Integer, Double> lets you get Integers as values, but also use them as keys for Doubles.

vast pine
#

ok