Collections public static SortedMap unmodifiableSortedMap(SortedMap m) Method Example Program


Returns an unmodifiable view of the specified sorted map. 

Program

package com.candidjava;

import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 
 * @author karthikeyan.T
 * @description the following code shows how to use
 *              Collections.unmodifiableSortedMap(SortedMap <K,? extends V> m)
 *              method.
 */
public class CollectionsUnmodifiableSortedMap {
	public static void main(String[] s) {
		SortedMap<String, String> map = new TreeMap<String, String>();
		map.put("1", "Karthik");
		map.put("5", "Mohan");
		map.put("3", "Kamal");
		System.out.println("Initial sorted map value: " + map);
		Map<String, String> unmodsortmap = Collections
				.unmodifiableSortedMap(map);
		unmodsortmap.put("4", "Modify");
	}
}

Output

Initial sorted map value: {1=Karthik, 3=Kamal, 5=Mohan}
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.put(Unknown Source)
at com.candidjava.CollectionsUnmodifiableSortedMap.main(CollectionsUnmodifiableSortedMap.java:24)

Explanation

public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
Returns 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.

Parameters:
m - the sorted map for which an unmodifiable view is to be returned.
Returns:
an unmodifiable view of the specified sorted map.


Related Post

Comments


©candidjava.com