9. 44. 1. Read-Only Collections |
|
public static Collection unmodifiableCollection(Collection c)
public static List unmodifiableList(List l)
public static Map unmodifiableMap(Map m)
public static Set unmodifiableSet(Set s)
public static SortedMap unmodifiableSortedMap(SortedMap m)
public static SortedSet unmodifiableSortedSet(SortedSet s)
|
|
To use these factory methods |
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String[] a) {
Set s = new HashSet();
s.add("A");
s.add("B");
s.add("C");
s.add("D");
s.add("E");
s.add("F");
s.add("H");
Collections.unmodifiableSet(s);
s = Collections.unmodifiableSet(s);
s.clear();
}
}
|
|
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.clear(Collections.java:1037)
at MainClass.main(MainClass.java:20) |