Package modernfeatures.java9.collections
Class CollectionFactoryMethodsDemo
java.lang.Object
modernfeatures.java9.collections.CollectionFactoryMethodsDemo
Demonstrates the use of Java 9 collection factory methods.
Java 9 introduced convenient static factory methods for creating immutable collections:
List.of()
Set.of()
Map.of()
UnsupportedOperationException
.
Example usage:
List<String> names = List.of("Alice", "Bob", "Carol"); Set<Integer> numbers = Set.of(1, 2, 3, 4, 5); Map<String, Integer> ages = Map.of("Alice", 30, "Bob", 25);
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
CollectionFactoryMethodsDemo
public CollectionFactoryMethodsDemo()
-
-
Method Details
-
main
Demonstrates creation of immutable collections using Java 9 factory methods, and shows that modification attempts result in exceptions.This method performs the following steps:
- Creates an immutable
List
usingList.of()
and prints it. - Creates an immutable
Set
usingSet.of()
and prints it. - Creates an immutable
Map
usingMap.of()
and prints it. - Attempts to modify each collection to demonstrate that they throw
UnsupportedOperationException
.
- Parameters:
args
- command-line arguments (not used)
- Creates an immutable
-