// collectiontopics/Unsupported.java // (c)2021 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Unsupported operations in Java collections import java.util.*; public class Unsupported { static void check(String description, Runnable tst) { try { tst.run(); } catch(Exception e) { System.out.println(description + "(): " + e); } } static void test(String msg, List list) { System.out.println("--- " + msg + " ---"); Collection c = list; Collection subList = list.subList(1,8); // Copy of the sublist: Collection c2 = new ArrayList<>(subList); check("retainAll", () -> c.retainAll(c2)); check("removeAll", () -> c.removeAll(c2)); check("clear", () -> c.clear()); check("add", () -> c.add("X")); check("addAll", () -> c.addAll(c2)); check("remove", () -> c.remove("C")); // The List.set() method modifies the value but // doesn't change the size of the data structure: check("List.set", () -> list.set(0, "X")); } public static void main(String[] args) { List list = Arrays.asList( "A B C D E F G H I J K L".split(" ")); test("Modifiable Copy", new ArrayList<>(list)); test("Arrays.asList()", list); test("unmodifiableList()", Collections.unmodifiableList( new ArrayList<>(list))); } } /* Output: --- Modifiable Copy --- --- Arrays.asList() --- retainAll(): java.lang.UnsupportedOperationException removeAll(): java.lang.UnsupportedOperationException clear(): java.lang.UnsupportedOperationException add(): java.lang.UnsupportedOperationException addAll(): java.lang.UnsupportedOperationException remove(): java.lang.UnsupportedOperationException --- unmodifiableList() --- retainAll(): java.lang.UnsupportedOperationException removeAll(): java.lang.UnsupportedOperationException clear(): java.lang.UnsupportedOperationException add(): java.lang.UnsupportedOperationException addAll(): java.lang.UnsupportedOperationException remove(): java.lang.UnsupportedOperationException List.set(): java.lang.UnsupportedOperationException */