java - Retreving the keys of Hashmap -
i trying retreive keys of hashmap
i using hashmap follows
hashmap<string, string> inner = new hashmap<string, string>(); hashmap<hashmap<string,string>, string> outer = new hashmap<hashmap<string,string>, string>();
i putting values in both hashmap follows:
inner.put("1","one"); inner.put("2","two"); inner.put("3","three"); outer.put(inner,"outer1"); outer.put(inner,"outer2");
now want output
1 1 outer1 1 one outer2 2 two outer1 2 two outer2 3 three outer1 3 three outer2
but unable this. can please me 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)); }); }
as mentioned in comments, second put on outer override first put (the key same in both). apart that, 1 way print out want follows:
outer.foreach((k,v) -> { k.foreach((k1, v1) -> system.out.println(k1 + " " + v1 + " " + v)); });
just iterate on outer hash map , iterate again on each key (inner hashmap).
hope helps.
Comments
Post a Comment