吹爆的一種深度拷貝方式

經過驗證,適合Map<People, List<People>>這種類型,當然people要實現序列化

public Object deepClone(Object source ) {

    //創建流對象
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    ByteArrayInputStream bis = null;
    ObjectInputStream ois = null;

        //序列化
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(source); //當前這個對象以對象流的方式輸出

        //反序列化
        bis = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bis);
        Object copyObj = ois.readObject();
        return copyObj;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章