سؤال

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