Deep copy Map in Groovy

后端 未结 5 2110
眼角桃花
眼角桃花 2020-12-15 17:26

How can I deep copy a map of maps in Groovy? The map keys are Strings or Ints. The values are Strings, Primitive Objects or other maps, in a recursive way.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 17:49

    For Json (LazyMap) this wokred for me

    copyOfMap = new HashMap<>()
    originalMap.each { k, v -> copyOfMap.put(k, v) }
    copyOfMap = new JsonSlurper().parseText(JsonOutput.toJson(copyOfMap))
    

    EDIT: Simplification by: Ed Randall

    copyOfMap = new JsonSlurper().parseText(JsonOutput.toJson(originalMap))
    

提交回复
热议问题