List<Integer> fibonacci = List.of(1, 1, 2, 3, 5, 8, 13); Set<String> faveLanguages = Set.of("Java", "TypeScript", "Rust"); Map<Integer, String> statusCodes = Map.of(200, "OK", 404, "Not Found", 500, "Internal Server Error");
1. Creating Immutable List
Before Java 9, creating an unmodifiable list involved multiple steps:
List<Integer> fibonacci = new ArrayList<>(); fibonacci.add(1); fibonacci.add(1); fibonacci.add(2); fibonacci.add(3); fibonacci.add(5); fibonacci.add(8); fibonacci.add(13); fibonacci = Collections.unmodifiableList(fibonacci);
With Java 9 and later:
List<Integer> fibonacci = List.of(1, 1, 2, 3, 5, 8, 13);
2. Creating Immutable Set
Before Java 9, creating an unmodifiable set was verbose:
Set<String> faveLanguages = new HashSet<>(); faveLanguages.add("Java"); faveLanguages.add("TypeScript"); faveLanguages.add("Rust"); faveLanguages = Collections.unmodifiableSet(faveLanguages);
With Java 9 and later:
Set<String> faveLanguages = Set.of("Java", "TypeScript", "Rust");
3. Creating Immutable Map
Before Java 9, creating an unmodifiable map required multiple steps:
Map<String, Integer> statusCodes = new HashMap<>(); statusCodes.put(200, "OK"); statusCodes.put(404, "Not Found"); statusCodes.put(500, "Internal Server Error"); statusCodes = Collections.unmodifiableMap(statusCodes);
With Java 9 and later:
Map<Integer, String> statusCodes = Map.of(200, "OK", 404, "Not Found", 500, "Internal Server Error");
ConvenienceFactoryMethodsDemo
-class, create and initialise a static list of descriptions (using a convenience factory method) for a weekly weather forecast (i.e. Sunny, Partly Cloudy, Cloudy, Rainy, Rainy, Partly Cloudy, Rainy, etc.).main
-method, attempt to add another weather condition like “Sunny”. What happens? Why?public class ConvenienceFactoryMethodsDemo { private static List<String> weeklyWeatherForecast = List.of("Sunny", "Partly Cloudy", "Cloudy", "Rainy", "Sunny", "Partly Cloudy", "Cloudy"); public static void main(String[] args) { // weeklyWeatherForecast.add("Sunny"); // ❌ Immutable list throws UnsupportedOperationException when you try to add an element for (Integer primeNum : Set.of(2, 3, 5, 7)) { System.out.println(primeNum); } System.out.printf("Total cost: $%.2f.\n", Map.of( "Clean Code", 45.99, "Refactoring", 47.50, "Design Patterns", 55.20, "Effective Java", 40.45, "Code Complete", 36.75, "The Pragmatic Programmer", 42.00, "Structure and Interpretation of Computer Programs", 38.95, "Introduction to Algorithms", 80.00, "The Art of Computer Programming", 250.00, "Head First Design Patterns", 44.10 // "You Don't Know JS", 29.99 ❌ Map.of() only allows 10 key-value pairs ).entrySet().stream() .peek(e -> System.out.printf("%s costs $%,.2f.\n", e.getKey(), e.getValue())) .mapToDouble(Map.Entry::getValue) .sum()); } }
Map.of()
is convenient for small maps and supports up to 10 key-value pairs. For larger maps, use Map.ofEntries()
, which accepts an unlimited number of Map.Entry
objects, allowing for greater flexibility but requiring explicit entry creation.List.of()
, Set.of()
, and Map.of()
for one-liner, immutable collection creation, enhancing code readability and reducing verbosity.Map.of()
support up to 10 entries. For more, use Map.ofEntries()
with explicit Map.Entry
objects for flexibility.