April 2018
Intermediate to advanced
246 pages
6h 11m
English
Many times we directly add or remove elements from a collection, which is returned from the factory method. This collection is immutable and adding items into these collection gives us an exception called UnSupportedOperationException.
To avoid such situations, we create immutable collection objects by using the collections.unmodifiableXXX() method. These methods are also tedious, such as writing multiple statements for adding individual items and then adding into it immutable List or Set or Map:
Before Java 9, List<String> asiaRegion = new ArrayList<String>();asiaRegion.add("India");asiaRegion.add("China");asiaRegion.add("SriLanka");List<String> unmodifiableAsiaRegionList = Collections.unmodifiableList(asiaRegion); ...