Java 9 introduced a new feature called “Factory methods for collections”, which provides a simpler and more concise way of creating immutable collections. In this article, we’ll explore this new feature and provide some examples and code snippets to help you get started.
Before we dive into the details, let’s briefly discuss the concept of immutable collections. An immutable collection is a collection that cannot be modified once it has been created. This can be useful in situations where you want to ensure that your data is read-only and cannot be accidentally modified by other parts of your program.
Prior to Java 9, creating immutable collections was a bit of a hassle. You had to use the Collections.unmodifiableXXX()
methods to wrap existing collections and make them immutable. However, with the new factory methods, creating immutable collections is now much easier.
Here’s an example of how you can create an immutable list using the new factory methods:
List<String> immutableList = List.of("apple", "banana", "orange");
This creates an immutable list with three elements: “apple”, “banana”, and “orange”. Note that you cannot add, remove, or modify elements in this list, as it is immutable.
Similarly, you can create immutable sets and maps using the Set.of()
and Map.of()
methods:
Set<String> immutableSet = Set.of("apple", "banana", "orange");
Map<String, Integer> immutableMap = Map.of("apple", 1, "banana", 2, "orange", 3);
In addition to creating immutable collections, the factory methods also provide a convenient way of creating small, mutable collections that can be used for temporary storage or as intermediate results in your code. Here’s an example of how you can create a mutable list using the List.of()
method:
List<String> mutableList = new ArrayList<>(List.of("apple", "banana", "orange"));
This creates a mutable list with the same elements as the previous immutable list. You can now add, remove, or modify elements in this list as needed.
One advantage of using the factory methods for collections is that they provide improved performance and reduced memory overhead compared to traditional collection creation methods. This is because the factory methods are optimized for creating small collections with a known size.
Let’s take a look at some more code examples:
Creating an empty immutable list:
List<String> emptyList = List.of();
Creating a small immutable list with a single element:
List<String> singletonList = List.of("apple");
Creating a mutable list with a predefined capacity:
List<String> mutableListWithCapacity = new ArrayList<>(10);
Creating a mutable map with a predefined capacity:
Map<String, Integer> mutableMapWithCapacity = new HashMap<>(10);
Creating a small immutable map with a single entry:
Map<String, Integer> singletonMap = Map.of("apple", 1);
Creating a small immutable map with two entries:
Map<String, Integer> smallMap = Map.of("apple", 1, "banana", 2);
Note that the factory methods for collections are limited to creating small collections with a known size. If you need to create larger collections, you should use the traditional collection creation methods instead.
In conclusion, the factory methods for collections introduced in Java 9 provide a simple and efficient way of creating immutable and mutable collections. By using these methods, you can write cleaner and more concise code that is less prone to errors and more efficient. If you’re still using the old Collections.unmodifiableXXX()
methods, it