<bdo id='1DAtt'></bdo><ul id='1DAtt'></ul>

    <tfoot id='1DAtt'></tfoot>
    <i id='1DAtt'><tr id='1DAtt'><dt id='1DAtt'><q id='1DAtt'><span id='1DAtt'><b id='1DAtt'><form id='1DAtt'><ins id='1DAtt'></ins><ul id='1DAtt'></ul><sub id='1DAtt'></sub></form><legend id='1DAtt'></legend><bdo id='1DAtt'><pre id='1DAtt'><center id='1DAtt'></center></pre></bdo></b><th id='1DAtt'></th></span></q></dt></tr></i><div id='1DAtt'><tfoot id='1DAtt'></tfoot><dl id='1DAtt'><fieldset id='1DAtt'></fieldset></dl></div>
  • <legend id='1DAtt'><style id='1DAtt'><dir id='1DAtt'><q id='1DAtt'></q></dir></style></legend>

        <small id='1DAtt'></small><noframes id='1DAtt'>

      1. Java - 遍历包含列表的地图

        时间:2023-10-14
        <i id='QWgcO'><tr id='QWgcO'><dt id='QWgcO'><q id='QWgcO'><span id='QWgcO'><b id='QWgcO'><form id='QWgcO'><ins id='QWgcO'></ins><ul id='QWgcO'></ul><sub id='QWgcO'></sub></form><legend id='QWgcO'></legend><bdo id='QWgcO'><pre id='QWgcO'><center id='QWgcO'></center></pre></bdo></b><th id='QWgcO'></th></span></q></dt></tr></i><div id='QWgcO'><tfoot id='QWgcO'></tfoot><dl id='QWgcO'><fieldset id='QWgcO'></fieldset></dl></div>

          <small id='QWgcO'></small><noframes id='QWgcO'>

              • <bdo id='QWgcO'></bdo><ul id='QWgcO'></ul>
                  <tbody id='QWgcO'></tbody>
                <legend id='QWgcO'><style id='QWgcO'><dir id='QWgcO'><q id='QWgcO'></q></dir></style></legend>

                <tfoot id='QWgcO'></tfoot>

                • 本文介绍了Java - 遍历包含列表的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  第一次来这里,所以我希望这是有道理的!

                  First time here so I hope this makes sense!

                  我有一个 Map,其中包含一个字符串作为它的键,以及一个字符串列表作为它的值.我需要遍历地图中每个列表中包含的所有变量.

                  I have a Map which contains a String as it's Key, and a List of Strings as it's Value. I need to iterate over all vlaues contained within each List within the Map.

                  所以,首先我想获得有效的密钥:

                  So, first I want to get the Keys, which works:

                  Set<String> keys = theMap.keySet();
                  

                  这将返回一个包含我所有密钥的集合.太好了:)

                  This returns me a Set containing all my Keys. Great :)

                  这是我卡住的地方 - 网络上的大多数信息似乎都假设我希望从 Key 返回的值是一个简单的 String 或 Integer,而不是另一个 Set,或者在这种情况下是一个列表.我尝试了 theMap.values() 但这不起作用,我尝试了一个 forloop/for:eachloop,但这些都没有成功.

                  This is where I've got stuck - most of the info on the web seems to assume that the values I'd want returned from the Key would be a simple String or Integer, not another Set, or in this case a List. I tried theMap.values() but that didn't work, and I tried a forloop / for:eachloop, and neither of those did the trick.

                  谢谢大家!

                  推荐答案

                  for(List<String> valueList : map.values()) {
                    for(String value : valueList) {
                      ...
                    }
                  }
                  

                  这才是真正的正常".方法来做到这一点.或者,如果您也需要密钥...

                  That's really the "normal" way to do it. Or, if you need the key as well...

                  for(Map.Entry<String, List<String>> entry : map.entrySet()) {
                    String key = entry.getKey();
                    for (String value : entry.getValue()) {
                      ...
                    }
                  }
                  

                  也就是说,如果您可以选择,您可能会对 Guava 的 ListMultimap,这是一个很像 Map<K, List<V>>,但有更多的功能——包括 Collection<V>values() 的行为与您所要求的完全一样,扁平化";多重映射中的所有值到一个集合中.(披露:我为 Guava 做出了贡献.)

                  That said, if you have the option, you might be interested in Guava's ListMultimap, which is a lot like a Map<K, List<V>>, but has a lot more features -- including a Collection<V> values() that acts exactly like what you're asking for, "flattening" all the values in the multimap into one collection. (Disclosure: I contribute to Guava.)

                  这篇关于Java - 遍历包含列表的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如果元素重复,为什么 Set.of() 会抛出 IllegalArgumentException? 下一篇:组合两个 Set 时出现 java.lang.UnsupportedOperationException

                  相关文章

                  1. <i id='NbQkm'><tr id='NbQkm'><dt id='NbQkm'><q id='NbQkm'><span id='NbQkm'><b id='NbQkm'><form id='NbQkm'><ins id='NbQkm'></ins><ul id='NbQkm'></ul><sub id='NbQkm'></sub></form><legend id='NbQkm'></legend><bdo id='NbQkm'><pre id='NbQkm'><center id='NbQkm'></center></pre></bdo></b><th id='NbQkm'></th></span></q></dt></tr></i><div id='NbQkm'><tfoot id='NbQkm'></tfoot><dl id='NbQkm'><fieldset id='NbQkm'></fieldset></dl></div>
                      <tfoot id='NbQkm'></tfoot>
                        <bdo id='NbQkm'></bdo><ul id='NbQkm'></ul>

                      <small id='NbQkm'></small><noframes id='NbQkm'>

                      <legend id='NbQkm'><style id='NbQkm'><dir id='NbQkm'><q id='NbQkm'></q></dir></style></legend>