import java.util.*; public class MutabilityJdk9 { void testList() { List list = List.of(1,2,3); if(list.size() > 5) { System.out.println("impossible"); } list.sort(null); } void testSet() { Set set = Set.of("foo", "bar", "baz", "qux"); if(set.size() == 4) { System.out.println("always"); } set.add("oops"); } void testMap() { Map map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5); if(map.size() != 5) { System.out.println("never"); } map.put("foo", 6); } void testMapOfEntries() { Map map = Map.ofEntries(Map.entry("x", 1), Map.entry("y", 2)); if(map.size() == 2) { System.out.println("you bet!"); } map.remove("x"); } // IDEA-167940 void testImmutable(String a, String b){ List.of(a).add(b); //java 9 collection literals do not accept modification Collections.singletonList(a).add(b); //singleton should not be modified } void testVarArg(String[] str) { List list = List.of(str); if(str.length > 2) { list.add("foo"); } } }