문제

Is there a more "Groovy" way or a oneliner for doing this?

def map1 = [:], map2= [foo: "bar"]
if (map1.submap) {
    map1.submap << map2
}
else {
    map1.submap = map2
}
도움이 되었습니까?

해결책

use withDefault:

def map1 = map1.withDefault { [ foo: 'bar' ] } 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top