简体   繁体   English

检索Hashmap的键

[英]Retrieving the keys of Hashmap

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));
});

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. 希望能帮助到你。

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. 但是您无法获得所需的结果,因为您在HashMap中放置了相同的KEY ,因此HshMap替换了键和值。

If you desplay the size of your HashMap you will find just one not two: 如果减小HashMap的大小,则只会发现一个而不是两个:

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 因此,如果要获取所需的内容,则应该更改密钥,例如在第一个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. 希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM