java.util.Collections.unmodifiableSet() 方法


java.util.Collections.unmodifiableSet() 方法

package com.codingdict;

import java.util.*;

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

      // create set
      Set<String> set = new HashSet<String>();

      // populate the set
      set.add("Welcome");
      set.add("to");
      set.add("TP");

      System.out.println("Initial set value: "+set);

      // create unmodifiable set
      Set unmodset = Collections.unmodifiableSet(set);

      // try to modify the set
      unmodset.add("Hello");
   }
}