Deep copy Map in Groovy

后端 未结 5 2111
眼角桃花
眼角桃花 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 18:15

    An easy way is this:

    // standard deep copy implementation
    def deepcopy(orig) {
         bos = new ByteArrayOutputStream()
         oos = new ObjectOutputStream(bos)
         oos.writeObject(orig); oos.flush()
         bin = new ByteArrayInputStream(bos.toByteArray())
         ois = new ObjectInputStream(bin)
         return ois.readObject()
    }
    

提交回复
热议问题