java中深復(fù)制知識(shí)點(diǎn)詳解
在正式開(kāi)始深復(fù)制的講解之前,我們先來(lái)理解一下概念。假設(shè)一個(gè)物品需要批量生產(chǎn),但是這個(gè)物品還配有贈(zèng)品,生產(chǎn)的時(shí)候需要把贈(zèng)品也列在計(jì)劃內(nèi)。所謂深復(fù)制的原理就是這樣,我們不能只復(fù)制屬性,包括引用之類(lèi)的附帶也需要被復(fù)制。下面小編就為大家?guī)?lái)深復(fù)制的兩種不同方法。
1.序列化實(shí)現(xiàn)如下為谷歌Gson序列化HashMap,實(shí)現(xiàn)深度復(fù)制的例子:
public class CopyDeepMapTest { public static void main(String[] args) { HashMap<Integer, User> userMap = new HashMap<>(); userMap.put(1, new User('jay', 26)); userMap.put(2, new User('fany', 25)); //Shallow clone Gson gson = new Gson(); String jsonString = gson.toJson(userMap); Type type = new TypeToken<HashMap<Integer, User>>(){}.getType(); HashMap<Integer, User> clonedMap = gson.fromJson(jsonString, type); //Same as userMap System.out.println(clonedMap); System.out.println('nChanges DO NOT reflect in other map n'); //Change a value is clonedMap clonedMap.get(1).setName('test'); //Verify content of both maps System.out.println(userMap); System.out.println(clonedMap); }}
運(yùn)行結(jié)果:
{1=User{name=’jay’, age=26}, 2=User{name=’fany’, age=25}}Changes DO NOT reflect in other map {1=User{name=’jay’, age=26}, 2=User{name=’fany’, age=25}}{1=User{name=’test’, age=26}, 2=User{name=’fany’, age=25}}
從運(yùn)行結(jié)果看出,對(duì)cloneMap修改,userMap沒(méi)有被改變,所以是深度復(fù)制。
2.list深復(fù)制兩個(gè)list數(shù)據(jù)相同但是地址值不同,A和B是獨(dú)立的兩個(gè)list,A改變了B不會(huì)改變,B改變了A也不會(huì)改變
深復(fù)制的工具類(lèi)方法:
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {ByteArrayOutputStream byteOut = new ByteArrayOutputStream();ObjectOutputStream out = new ObjectOutputStream(byteOut);out.writeObject(src);ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());ObjectInputStream in = new ObjectInputStream(byteIn);@SuppressWarnings('unchecked')List<T> dest = (List<T>) in.readObject();return dest;}
關(guān)于深拷貝的一個(gè)擴(kuò)展實(shí)例:
public class YuelyLog implements Serializable,Cloneable { private Attachment attachment; private String name; private String date; @Override protected YuelyLog clone() throws CloneNotSupportedException { return (YuelyLog)super.clone(); } public Attachment getAttachment() { return attachment; } public void setAttachment(Attachment attachment) { this.attachment = attachment; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } /** * 使用序列化技術(shù)實(shí)現(xiàn)深拷貝 * @return */ public YuelyLog deepClone() throws IOException,ClassNotFoundException{ //將對(duì)象寫(xiě)入流中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(this); //從流中取出 ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); return (YuelyLog)objectInputStream.readObject(); }}
相關(guān)文章:
1. Python編寫(xiě)nmap掃描工具2. Java向Runnable線程傳遞參數(shù)方法實(shí)例解析3. python 爬取嗶哩嗶哩up主信息和投稿視頻4. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值5. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站6. php5.6不能擴(kuò)展redis.so的解決方法7. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送8. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】9. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總10. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密
