list的淺複製和深複製

最近用到list的深淺複製,做個記錄
需求是複製當前list,進行操作而不改變原有的list的數據

先初始化一個list

    public static List<Person> init() {
        List<Person> personList = new ArrayList<Person>();
        Person p1 = new Person(20, "小李");
        Person p2 = new Person(21, "小王");
        Person p3 = new Person(22, "小趙");
        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        return personList;
    }

    public static void printList(List<Person> list, String str) {
        System.out.println(str + "List---begin---");
        for (Person person : list) {
            System.out.println(person.getName());
        }
        System.out.println(str + "List---end---");
    }

  

淺複製

一.遍歷循環複製

對初始化的list進行循環複製操作

  public static void test01(List<Person> personList) {
        List<Person> destList = new ArrayList<Person>(personList.size());
        for (Person p : personList) {
            destList.add(p);
        }
        personList.get(0).setName("修改小李");
        printList(personList, "原");
        printList(destList, "目標");
    }

結果:
原List—begin—
修改小李
小王
小趙
原List—end—
目標List—begin—
修改小李
小王
小趙
目標List—end—

二.使用List實現類的構造方法

 public static void test02(List<Person> personList) {
        List<Person> destList=new ArrayList<Person>(personList);
        destList.get(0).setName("修改小李");
        printList(personList, "原");
        printList(destList, "目標");
    }

結果:
原List—begin—
修改小李
小王
小趙
原List—end—
目標List—begin—
修改小李
小王
小趙
目標List—end—

三.使用list.addAll()方法

 public static void test03(List<Person> personList) {
        List<Person> destList=new ArrayList<Person>(personList.size());
        destList.addAll(personList);
        destList.get(0).setName("修改小李");
        printList(personList, "原");
        printList(destList, "目標");
    }

結果:
原List—begin—
修改小李
小王
小趙
原List—end—
目標List—begin—
修改小李
小王
小趙
目標List—end—

四.使用System.arraycopy()方法

 public static void test04(List<Person> personList) {
        Person[] srcPersons=personList.toArray(new Person[0]);
        Person[] destPersons=new Person[srcPersons.length];
        System.arraycopy(srcPersons, 0, destPersons, 0, srcPersons.length);
        List<Person> destList=Arrays.asList(destPersons);
        destList.get(0).setName("修改小李");
        printList(personList, "原");
        printList(destList, "目標");

    }

結果:
原List—begin—
修改小李
小王
小趙
原List—end—
目標List—begin—
修改小李
小王
小趙
目標List—end—

五.java.util.Collections.copy()方法

 public static void test05(List<Person> personList) {
        List<Person> destList=new ArrayList<Person>();
        CollectionUtils.addAll(destList, new Person[personList.size()]);
        Collections.copy(destList, personList);
        destList.get(0).setName("修改小李");
        printList(personList, "原");
        printList(destList, "目標");
    }

結果:
原List—begin—
修改小李
小王
小趙
原List—end—
目標List—begin—
修改小李
小王
小趙
目標List—end—

深複製

六.使用序列化方法(相對靠譜的方法)

親測有效,,,首先你的person需要實現序列化

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;    
}  

    public static void test06(List<Person> personList) {
        List<Person> destList = new ArrayList<>();
        try {
            destList = deepCopy(personList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        destList.get(0).setName("修改小李");
        printList(personList, "原");
        printList(destList, "目標");
    }

原List—begin—
小李
小王
小趙
原List—end—
目標List—begin—
修改小李
小王
小趙
目標List—end—

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