Copying a HashMap in Java

前端 未结 11 1064
灰色年华
灰色年华 2020-11-29 22:02

I am trying to keep a temporary container of a class that contains member :

HashMap myobjectHashMap

A class called my

11条回答
  •  时光说笑
    2020-11-29 22:37

    Since the OP has mentioned he does not have access to the base class inside of which exists a HashMap - I am afraid there are very few options available.

    One (painfully slow and resource intensive) way of performing a deep copy of an object in Java is to abuse the 'Serializable' interface which many classes either intentionally - or unintentionally extend - and then utilise this to serialise your class to ByteStream. Upon de-serialisation you will have a deep copy of the object in question.

    A guide for this can be found here: https://www.avajava.com/tutorials/lessons/how-do-i-perform-a-deep-clone-using-serializable.html

提交回复
热议问题