2

Here is what I have:

HashMap<String,HashMap<Integer,Integer>> data =
            new HashMap<String,HashMap<Integer,Integer>>();

But I am having trouble adding values to this, because the inner hashmap doesn't have a name (note: it isn't supposed to). I'm actually trying to add an array list to the first Integer in HashMap So I am trying something like:

data.put(var, data.get(array.get(x), y));

Which it very much doesn't like and I'm totally clueless as to how to do it.

2
  • what is the value of array.get(x) and y? Commented Mar 24, 2013 at 17:53
  • I'd suggest u to take a look on Guava libraries Mulitmap. code.google.com/p/guava-libraries/wiki/… Commented Mar 24, 2013 at 17:56

4 Answers 4

5

Note that

HashMap<String,HashMap<Integer,Integer>> data =
            new HashMap<String,HashMap<Integer,Integer>>();

only creates the "outer" HashMap instance. After this statement you have an empty HashMap that takes Strings as keys and HashMap<Integer, Integer> as value.

You can add an instance of HashMap<Integer, Integer> to data with this:

data.put("myKey", new HashMap<Integer, Integer>());

After that you can add Integer values to the second HashMap:

data.get("myKey").put(123, 456); // use 123 as key and 456 as value

Get the values back:

data.get("myKey").get(123); // returns 456
Sign up to request clarification or add additional context in comments.

Comments

1

You have to get the inner hash map first:

HashMap<Integer,Integer> innerData = data.get(var);

Then you can put your value into it:

innerData.put(x, y);

Comments

0

Just do it like this:

data.put( var, new HashMap(intKey, intVal));

where intKey and intVal are Integer type Key and Integer type value.

Comments

0
HashMap<String,HashMap<Integer,Integer>> data =
            new HashMap<String,HashMap<Integer,Integer>>();
((Map)data.get( "keyname" )).get(1);

and subsequently:

   ((Map)data.get( "keyname" )).get( 1 ).put(2);

1 Comment

Note that the second has map uses integers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.