The Java definition returns a List that cannot be modified or deleted

 

Why did I suddenly share this, I also saw it from the source code of mybatis, so I want to share it:

org.apache.ibatis.plugin.InterceptorChain

 
Use Collections.unmodifiableList();

Example:

    public static void main(String[] args) {
        
        List<String> canNotEditList = getCanNotEditList();
        canNotEditList.add("收藏");
        System.out.println(canNotEditList.toString());
    }
    
    private static List<String> getCanNotEditList() {
        List<String> canNotEditList = new ArrayList<>();
        canNotEditList.add("请");
        canNotEditList.add("点");
        canNotEditList.add("赞");

       return  Collections.unmodifiableList(canNotEditList);
    }

running result:

Don't move.

 

Guess you like

Origin blog.csdn.net/qq_35387940/article/details/131601766