Preface
()
is a static method introduced in Java 9 to create immutable lists (ImmutableList
). This means once used()
Once a list is created, you cannot add, delete or modify elements to it. This method provides a convenient way to create an immutable list of zero or more elements.
()
There are several overloaded versions of the method that allow you to pass zero to ten elements as parameters. If you need to create a list with more than ten elements, you can use()
Methods to combine smaller lists, or use other set constructors (e.g.()
Heel()
, although this is not a purely immutable list, as it can still be modified by the original array).
Basic usage
// Create an empty immutable listList<String> emptyList = (); // Create an immutable list containing a single elementList<Integer> singleElementList = (1); // Create an immutable list with multiple elementsList<String> multipleElementsList = ("apple", "banana", "cherry");
Things to note
Immutability: Once created, the content of the list cannot be changed. Attempting to add, delete or modify elements will cause
UnsupportedOperationException
。performance: Due to immutability,
()
The created list may not be optimal in performance, especially when performing a lot of modifications. However, for scenarios where immutable sets are required, this approach provides simplicity and security.Type safety:because
()
is a generic method that ensures the type safety of lists.Use scenarios:
()
Very suitable for creating constant lists, configuration lists, or immutable lists passed as method parameters.Elemental limitations: Directly passed
()
The method can pass up to ten elements. If you need more elements, consider using other collection constructors or combining multiple()
Call.
Example: Combining multiple () calls
// Create two smaller immutable listsList<Integer> firstHalf = (1, 2, 3, 4); List<Integer> secondHalf = (5, 6, 7, 8, 9, 10); // Use Stream to combine them into a larger immutable listList<Integer> combinedList = ((), ()) .collect(((), Collections::unmodifiableList)); // Note: The combinedList above is not actually a purely ()-created immutable list.// But it achieves a similar effect by using (). // A simpler but non-pure immutable approach (if no complete immutability is required)List<Integer> simplerCombinedList = new ArrayList<>(firstHalf); (secondHalf); // Notice:simplerCombinedList It's variable
The abovesimplerCombinedList
Examples are variable, andcombinedList
The example is passed()
Try to create an immutable list, but it is not made by()
Created directly. For completely by()
Created immutable lists, you should pass elements directly to()
, or consider using another design if the number of elements exceeds the limit.
Appendix: The difference between () and ()
()
and()
Both are two different methods used in Java to create lists, and they can be used to create lists. But they also have some significant differences. The author will explain their main differences below
1. Immutability and variability
()
- The created list is immutable
- Once this list is created, the elements in the list cannot be modified (add, delete or change elements)
- When we try to modify the list constructed by this method, an error will be reported
UnsupportedOperationException
()
- The created list is a fixed-size list based on the original array (fixed-size)
- The size of the list is fixed and elements cannot be added or removed.
- However, the elements in the list are mutable and the elements can be modified
- The contents of the underlying array can be modified indirectly, for example, if the list contains mutable objects (e.g.
ArrayList
or custom objects), then the internal state of these objects can be changed
2. Performance comparison
()
- Optimized for small numbers of elements, usually better suited to creating fixed and immutable small numbers of elements
- Internal implementations may vary depending on the number of elements (for example, use special classes for single elements, two elements, etc.)
()
- Based on existing array implementations, the performance overhead is low, especially when dealing with large numbers of elements
- However, since it is fixed-size, performance may not be as good as if the list is dynamically resized.
ArrayList
3. Applicable scenarios
()
- Suitable for scenarios where immutable lists are required. For example as a constant, configuration data or any data set that does not wish to be modified
()
- Suitable for scenarios where existing arrays need to be converted to lists, or when you know that the list size will not change but requires list functionality (such as iteration, index access)
Summarize
This is the article about the use examples and precautions of Java 9. For more information about using Java 9, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!