java.util.Collections.unmodifiableCollection() 方法


java.util.Collections.unmodifiableCollection() 方法

package com.codingdict;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {

      // create array list
      List<Character> list = new ArrayList<Character>();

      // populate the list
      list.add('X');
      list.add('Y');

      System.out.println("Initial list: "+ list);

      Collection<Character> immutablelist = Collections.unmodifiableCollection(list);

      // try to modify the list
      immutablelist.add('Z');      
   }
}