VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/collections-unmodifiablesortedmap-method-in-java-with-examples/

⇱ Collections unmodifiableSortedMap() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collections unmodifiableSortedMap() method in Java with Examples

Last Updated : 2 May, 2023

The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specified sorted map. Attempts to modify the returned sorted map, whether direct, via its collection views, or via its subMap, headMap, or tailMap views, result in an UnsupportedOperationException. The returned sorted map will be serializable if the specified sorted map is serializable. 

Syntax:

public static <K, V> SortedMap<K, V>
 unmodifiableSortedMap(SortedMap<K, ? extends V> m)

Parameters: This method takes the sorted map as a parameter for which an unmodifiable view is to be returned. 

Return Value: This method returns an unmodifiable view of the specified sorted map. 

Below are the examples to illustrate the unmodifiableSortedMap() method 

Example 1: 

Output:
Initial sorted map value: {First=10, Second=20, Third=30}

Example 2: For UnsupportedOperationException 

Output:
unmodifiableSortedMap value: {First=10, Second=20, Third=30}

Trying to modify the unmodifiable SortedMap
Exception thrown : java.lang.UnsupportedOperationException
Comment

Explore