Java 10 Unmodifiable Collections Changes

One of the new features added by Java 10 is some new methods for creating unmodifiable collections. In this article, I will be covering the Java 10 Unmodifiable Collections.

What is an Unmodifiable Collection?

As the name implies, an unmodifiable Collection is a Collection that cannot be modified. So if you create an unmodifiable collection and then try to add or remove a value from it, Java throws an Exception

What is the use of an Unmodifiable Collection

Sometimes, you may need to create a read only copy of a Collection. Unmodifiable collections are useful in such scenarios.

How to create an Unmodifiable Collection using Java 10

Java 10 has added a copyOfmethod to all the Collection interfaces. You can use this to create an unmodifiable Collection. The following code demonstrates this:

List<Integer> numberList = Arrays.asList(2,4,6,8,10);numberList.add(12); // Line 1 - no exception    List<Integer> unmodifiableList = List.copyOf(numberList);unmodifiableList.add(14); //Line 2 - throws exception

This code creates an Integer List. Line 1 adds a value to the List. This does not cause any exception. Then, the code uses the List.copyOfmethod. This creates an unmodifiable List. Line 2 then tries to add a value to this List. However, this code throws an UnsupportedOperationException.

 

Difference between copyOf and Collections.unmodifiableList

Prior to Java 10, you could create unmodifiable Collections via the Collections class.  The Collectionsclass has several utility methods that return an unmodifiable Collection. For example, the Collections.unmodifiableListreturns an unmodifiable List.  So you may wonder why Java 10 has added the copyOfmethod and what is the difference between copyOfand Collections.unmodifiableList

The Collections.unmodifiableListmethod returns an unmodifiable view of the source List. So if the source List is modified, these changes are reflected in the unmodifiable List. The copyOfmethods on the other hand, return a read-only copy of the source Collection. So even if the source Collection is modified, the unmodifiable Collection does not change.

toUnmodifiable method

Java 10 has also added a toUnmodifiablemethod on the Collectors class. This helps to create an unmodifiable Collection from a Stream. The following code demonstrates this:

Stream<String> myStream = Stream.of("red","blue","green");  List<String> colours = myStream.collect(Collectors.toUnmodifiableList());

This code creates a Stream of Strings. It then uses the Stream.collectmethod to convert the Stream to a List. It specifies the Collectors.unmodifiableList()which returns a Collectorthat creates an unmodifiable List.

 

Conclusion

So in this article, we saw the Java 10 copyOfmethod that helps to create unmodifiable Collection. We also understood the difference between Collections.unmodifiableListmethod and the copyOfmethod. Finally, we took a look at the Collectors.toUnmodifiablemethod also added by Java 10.

 

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set