what is the sense of final ArrayList?

前端 未结 12 493
日久生厌
日久生厌 2020-11-29 00:16

Which advantages/disadvantages we can get by making ArrayList (or other Collection) final? I still can add to ArrayList new elements, remove elements and update it. But what

12条回答
  •  失恋的感觉
    2020-11-29 00:50

    To get a really immutable list, you will have to make deep copies of the contents of the list. UnmodifiableList would only render the list of references somewhat immutable. Now making a deep copy of the List or array will be tough on memory with the growing size. You can make use of serialization/deserialization and store the deep copy of array/list into a temp file. The setter would not be available as the member varaible needs to be immutable. The getter would serialize the member variable into a file and then desialize it to get a deep copy. Seraialization has an innate nature of going into the depths of an object tree. This would ensure complete immutability at some performance cost though.

     package com.home.immutable.serial;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    public final class ImmutableBySerial {
    
        private final int num;
        private final String str;
        private final ArrayList immutableList;
    
        ImmutableBySerial(int num, String str, ArrayList list){
            this.num = num;
            this.str = str;
            this.immutableList = getDeepCloned(list);
        }
    
        public int getNum(){
            return num;
        }
    
        public String getStr(){
            return str;
        }
    
        public ArrayList getImmutableList(){
            return getDeepCloned(immutableList);
        }
    
        private ArrayList getDeepCloned(ArrayList list){
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            FileInputStream fis = null;
            ObjectInputStream ois = null;
            ArrayList clonedObj = null;
            try {
                 fos = new FileOutputStream(new File("temp"));
                 oos = new ObjectOutputStream(fos);
                 oos.writeObject(list);
                 fis = new FileInputStream(new File("temp"));
                 ois = new ObjectInputStream(fis);
                 clonedObj = (ArrayList)ois.readObject();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    oos.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return clonedObj;
        }
    }
    

提交回复
热议问题