The toUnmodifiableList()
method in Java, part of the java.util.stream.Collectors
class, is used to collect elements of a stream into an unmodifiable List
. This method is useful when you need to create a list that should not be modified after its creation.
Table of Contents
- Introduction
toUnmodifiableList()
Method Syntax- Understanding
toUnmodifiableList()
- Examples
- Basic Usage
- Using
toUnmodifiableList()
with Custom Objects
- Real-World Use Case
- Conclusion
Introduction
The toUnmodifiableList()
method returns a Collector
that accumulates the input elements into an unmodifiable List
. This method is particularly useful when you want to create a read-only list to prevent any modifications after its creation.
toUnmodifiableList() Method Syntax
The syntax for the toUnmodifiableList()
method is as follows:
public static <T> Collector<T, ?, List<T>> toUnmodifiableList()
Parameters:
- This method does not take any parameters.
Returns:
- A
Collector
that collects the input elements into an unmodifiableList
.
Throws:
- This method does not throw any exceptions.
Understanding toUnmodifiableList()
The toUnmodifiableList()
method allows you to collect the elements of a stream into an unmodifiable List
. The resulting list is immutable, meaning that attempts to modify it will result in an UnsupportedOperationException
.
Examples
Basic Usage
To demonstrate the basic usage of toUnmodifiableList()
, we will create a stream of integers and collect them into an unmodifiable list.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ToUnmodifiableListExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Collect the numbers into an unmodifiable list
List<Integer> unmodifiableList = numbers.stream()
.collect(Collectors.toUnmodifiableList());
System.out.println("Unmodifiable List: " + unmodifiableList);
// Attempting to modify the list will result in an UnsupportedOperationException
// unmodifiableList.add(6); // Uncommenting this line will throw an exception
}
}
Output:
Unmodifiable List: [1, 2, 3, 4, 5]
Using toUnmodifiableList()
with Custom Objects
This example shows how to use toUnmodifiableList()
with a stream of custom objects to collect them into an unmodifiable list.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ToUnmodifiableListCustomObjectExample {
static class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " ($" + price + ")";
}
}
public static void main(String[] args) {
List<Product> products = Arrays.asList(
new Product("Product A", 10.0),
new Product("Product B", 20.5),
new Product("Product C", 15.8)
);
// Collect the products into an unmodifiable list
List<Product> unmodifiableProductList = products.stream()
.collect(Collectors.toUnmodifiableList());
System.out.println("Unmodifiable Product List: " + unmodifiableProductList);
// Attempting to modify the list will result in an UnsupportedOperationException
// unmodifiableProductList.add(new Product("Product D", 30.0)); // Uncommenting this line will throw an exception
}
}
Output:
Unmodifiable Product List: [Product A ($10.0), Product B ($20.5), Product C ($15.8)]
Real-World Use Case
Collecting Read-Only Employee Names
In real-world applications, the toUnmodifiableList()
method can be used to collect the names of employees into a read-only list.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeNameListExample {
static class Employee {
String name;
int age;
Employee(String name, int age) {
this.name = name;
this.age = age;
}
String getName() {
return name;
}
}
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Alice", 30),
new Employee("Bob", 25),
new Employee("Charlie", 35)
);
// Collect the employee names into an unmodifiable list
List<String> nameList = employees.stream()
.map(Employee::getName)
.collect(Collectors.toUnmodifiableList());
System.out.println("Unmodifiable Name List: " + nameList);
// Attempting to modify the list will result in an UnsupportedOperationException
// nameList.add("David"); // Uncommenting this line will throw an exception
}
}
Output:
Unmodifiable Name List: [Alice, Bob, Charlie]
Conclusion
The Collectors.toUnmodifiableList()
method is used to collect the elements of a stream into an unmodifiable List
. This method is particularly useful for creating read-only lists to prevent any modifications after their creation. By understanding and using this method, you can efficiently manage immutable collection operations in your Java applications.
Comments
Post a Comment
Leave Comment