0

I am trying to retrieve the keys of hashmap.

I am using to hashmap as follows:

HashMap<String, String> inner = new HashMap<String, String>();
HashMap<HashMap<String,String>, String> outer = new HashMap<HashMap<String,String>, String>();

I am putting values in both the hashmap as follows:

inner.put("1", "one");
inner.put("2", "two");
inner.put("3", "three");

outer.put(inner, "outer1");
outer.put(inner, "outer2");

Now I want to get the output as

1 one outer1
1 one outer2
2 two outer1
2 two outer2
3 three outer1
3 three outer2

But I am unable to get this. Can you please help me to solve this.

Edited code:

HashMap<String, String> inner = new HashMap<>();
HashMap<String, String> inner1 = new HashMap<>();
HashMap<HashMap<String, String>, String> outer = new HashMap<>();

outer.put(inner, "outer1");
outer.put(inner1, "outer2");

inner1.put("1", "one");
inner1.put("2", "two");
inner1.put("3", "three");
inner1.put("4", "three");

inner.put("1", "one");
inner.put("2", "two");
inner.put("3", "three");
inner.put("4", "three");

 outer.forEach((k,v) -> {
    k.forEach((k1, v1) -> System.out.println(k1 + " " + v1 + " " + v));
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
joe
  • 15
  • 1
  • 5
  • Can you post some example code you are using to get an output? And what is your error? – Matt Clark Nov 05 '16 at 17:32
  • Also, why is the `HashMap` the key of your outer? The string should be the key, with the map the value.. – Matt Clark Nov 05 '16 at 17:33
  • I have tried using for in which I have ilterated the keyset values as follows: for(HashMap hash: outer.keyset()) { hash.keyset() } – joe Nov 05 '16 at 17:35
  • 1
    You need to iterate over key value pairs of your hashmaps http://stackoverflow.com/questions/585654/what-is-the-easiest-way-to-iterate-over-all-the-key-value-pairs-of-a-java-util-m – Pavel Nov 05 '16 at 17:36
  • @Pavel I tried using iterating like as below for(HashMap hash: outer.keyset()) { hash.keyset() } But was not able to get the desired result I was getting the result as 1 one outer1 2 two outer1 3 three outer1 can you please help me to solve this out – joe Nov 05 '16 at 17:40
  • @joe, the link shows how its supposed to be done, do you still not understand? – Pavel Nov 05 '16 at 17:41
  • 1
    the second put in outer will override the first (same key) – acontell Nov 05 '16 at 17:49
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Hot Licks Nov 05 '16 at 18:23

2 Answers2

0

As I mentioned in the comments, the second put over outer will override the first put (the key is the same in both). Apart from that, one way to print out what you want could be as follows:

outer.forEach((k,v) -> {
    k.forEach((k1, v1) -> System.out.println(k1 + " " + v1 + " " + v));
});

Just iterate over the outer hash map and iterate again over each key (inner hashmap).

Hope it helps.

acontell
  • 6,792
  • 1
  • 19
  • 32
0

You can use this way this work with me:

    for (HashMap<String, String> key : outer.keySet()) {
        for (String key2 : key.keySet()) {
            System.out.println(key2 + " " + key.get(key2) + " " + outer.get(key));
        }

Or this way:

outer.keySet().stream().forEach((key) -> {
    key.keySet().stream().forEach((key2) -> {
        System.out.println(key2 + " " + key.get(key2) + " " + outer.get(key));
    });
});

But you can't get the result you want, because you put in your HashMap the same KEY, so the HshMap replace the key and values.

If you desplay the size of your HashMap you will find just one not two:

System.out.println(outer.size());

So the Correct result

1 one outer2
2 two outer2
3 three outer2

Wrong result

1 one outer1
1 one outer2
2 two outer1
2 two outer2
3 three outer1
3 three outer2

So if you want to get what you want you should to change the key, like to add another thing to the first HashMap

inner.put("1", "one");
inner.put("2", "two");
inner.put("3", "three");

outer.put(inner, "outer1");
inner.put("4","three");
outer.put(inner, "outer2");

Hope this can help you.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • On running the above code I am getting output as 1 one outer2 2 two outer2 3 three outer2 But I want the output for outer 1 as well. – joe Nov 05 '16 at 18:11
  • Yes the result is correct, because the key is the same, i edit the answer you can learn it – Youcef LAIDANI Nov 05 '16 at 18:21
  • Hmm actually this is the sample code which I have given In my real code I have to take the values dynamically so how can I do the edditing which you have just told. Please help me to sort this out – joe Nov 05 '16 at 18:40
  • you can't create two HashMap?, i think this can help you! – Youcef LAIDANI Nov 05 '16 at 18:46
  • I have taken two hashmap but ept the keys and values same but still getting same result. I will edit my code. Can you pls provide me solution to this – joe Nov 05 '16 at 18:56
  • no create like this `HashMap, String> outer1 = new HashMap<>();` `HashMap, String> outer2 = new HashMap<>();` – Youcef LAIDANI Nov 05 '16 at 19:35
  • I tried this but unable to solve the issue. Can you please provide me the sample code to solve this issue. – joe Nov 06 '16 at 16:29