gpt4 book ai didi

loops - 在 groovy 中迭代 Map, Map>

转载 作者:行者123 更新时间:2023-12-04 17:49:39 24 4
gpt4 key购买 nike

嗨,我有复杂的结构

 Map<Map<String,String>, Map<String,String>> a

我想遍历它的所有元素。我试过:
for(Map.Entry<Map<String,String>, Map<String,String>> first:firstMap.keySet()) { 
...
}

错误是
Cannot cast object '{key1=value1, key2=value2, key3=value3, key4=value4}' with class 'java.util.LinkedHashMap' to class 'java.util.Map$Entry' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Map$Entry(java.util.LinkedHashMap)

如何迭代我的 map ?

最佳答案

keySet()只返回它的键,所以它是一个 Map<String, String> 的列表.如果你想遍历它的 Map.Entry ,放下 .keySet() :

for (Map.Entry<Map<String, String>, Map<String, String>> entry : firstMap) {
println "entry=$entry"
}

其他循环选项:
// iterate with two arguments
firstMap.each { Map<String, String> key, Map<String, String> value ->
println "key=$key, value=$value"
}

// iterate through entries
firstMap.each { Map.Entry<Map<String, String>, Map<String, String>> entry ->
println "entry=$entry"
}

// untyped
for (entry in firstMap) {
println entry
}

关于loops - 在 groovy 中迭代 Map<Map<String,String>, Map<String,String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com