Collections public static SortedSet unmodifiableSortedSet(SortedSet s) Method Example Program


Returns an unmodifiable view of the specified sorted set. This method allows modules to provide users with "read-only" access to internal sorted sets.

Program

package com.candidjava;

import java.util.Collections;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * 
 * @author karthikeyan.T
 * @description the following code shows how to use
 *              Collections.unmodifiableSortedSet(SortedSet <T> s) method.
 */
public class CollectionsUnmodifiableSortedSet {
	public static void main(String[] s) {
		SortedSet<String> set = new TreeSet<String>();
		set.add("karthik");
		set.add("mohan");
		set.add("kamal");
		System.out.println("Initial set value: " + set);
		Set<String> unmodsortset = Collections.unmodifiableSortedSet(set);
		unmodsortset.add("Hello");
	}
}

Output

Initial set value: [kamal, karthik, mohan]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
at com.candidjava.CollectionsUnmodifiableSortedSet.main(CollectionsUnmodifiableSortedSet.java:22)

Explanation

public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
Returns an unmodifiable view of the specified sorted set. This method allows modules to provide users with "read-only" access to internal sorted sets. Query operations on the returned sorted set "read through" to the specified sorted set. Attempts to modify the returned sorted set, whether direct, via its iterator, or via its subSet, headSet, or tailSet views, result in an UnsupportedOperationException.
The returned sorted set will be serializable if the specified sorted set is serializable.

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


Related Post

Comments


©candidjava.com