我正在嘗試執行以下操作:我有一個嵌套地圖(地圖中的地圖),并且我想找出在這些地圖中的任何一個中是否出現了選定的值(例如 null)。如果此值確實出現在任何地圖中,我希望該函式回傳一個 true 陳述句。考慮到這一點,我認為遞回函式可以解決問題:
public boolean search(def root) {
found = false
root.each{ def it ->
if (it.value instanceof Map) {
found = search(it.value)
} else if (it.value == null) {
println "value found"
found = true
}
}
return found
}
現在,當使用地圖對此進行測驗時,即使我的輸出表明我的函式正在執行正確的迭代,我也沒有得到正確的結果。誰能幫助我為什么會發生這種情況。提前非常感謝!
編輯:這是一個示例地圖,我沒有得到預期的結果。即使有一個值為 null 的值,搜索函式也會回傳一個 false 陳述句:
def map = [name: [name: [name:
[test: [selection : [pet : null]]],
age: 42, city: "New York"],
age: 42, city: [name: [name: [test:"New York"]]]],
age: 42, city: "New York"]
uj5u.com熱心網友回復:
public boolean search(def root) {
return root.find{
if (it.value instanceof Map) {
return search(it.value)
} else if (it.value == null) {
return true
}
return false
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/441376.html
上一篇:字典字典同時更新所有鍵
