82
Java Collections unmodifiableSortedMap() Method
The unmodifiableSortedMap() method of Java Collections class is used to get an unmodifiable view of the specified sorted map.
Syntax
Following is the declaration of unmodifiableSortedMap() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
m | It is the sorted map for which an unmodifiable view is to be returned. | Required |
Returns
The unmodifiableSortedMap() method returns an unmodifiable view of the specified sorted map.
Exceptions
NA
Example 1
Output:
Original Map: {1=1001, 2=1002, 3=1003, 4=1004} Unmodifiable Sorted Map: {1=1001, 2=1002, 3=1003, 4=1004, 5=1005}
Example 2
Output:
Original Map: {1=Whatsapp, 2=Twitter, 3=Facebook, 4=Instagram} Unmodifiable Sorted Map: {1=Whatsapp, 2=Twitter, 3=Facebook, 4=Instagram, 5=Hangout}
Example 3
Output:
Original Map: {1=one, 2=two} Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableMap.remove(Collections.java:1456) at myPackage.CollectionsUnmodifiableSortedMapExample3.main(CollectionsUnmodifiableSortedMapExample3.java:10)
Next TopicJava Collections Class