Java 定義は、変更または削除できないリストを返します。

 

なぜこれを突然共有したかというと、mybatis のソースコードからも見えたので共有したいと思います。

org.apache.ibatis.plugin.InterceptorChain

 
使用 Collections.unmodifiableList();

例:

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

実行結果:

動かないで下さい。

 

おすすめ

転載: blog.csdn.net/qq_35387940/article/details/131601766