Collections static SortedSet checkedSortedSet(SortedSet s,Class type) method Example Program


Returns a dynamically typesafe view of the specified sorted set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a sorted set contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all subsequent access to the sorted set takes place through the view, it is guaranteed that the sorted set cannot contain an incorrectly typed element.

Program

package com.candidjava;

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

/**
 * 
 * @author karthikeyan.T
 * @description :Returns a dynamically typesafe view of the specified sorted
 *              set. Any attempt to insert an element of the wrong type will
 *              result in an immediate ClassCastException.
 */
public class CheckedSortedSet {
	public static void main(String args[]) {
		SortedSet<String> css = new TreeSet<String>();
		css.add("karthik");
		css.add("mohan");
		css.add("kamal");
		css.add("hari");
		css.add("vinodh");
		css.add("anand");
		SortedSet<String> tset = Collections
				.checkedSortedSet(css, String.class);
		System.out.println(tset);
	}

}

Output

[anand, hari, kamal, karthik, mohan, vinodh]

Explanation

public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,Class<E> type)
Returns a dynamically typesafe view of the specified sorted set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a sorted set contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all subsequent access to the sorted set takes place through the view, it is guaranteed that the sorted set cannot contain an incorrectly typed element.
A discussion of the use of dynamically typesafe views may be found in the documentation for the checkedCollection method.
The returned sorted set will be serializable if the specified sorted set is serializable.
Since null is considered to be a value of any reference type, the returned sorted set permits insertion of null elements whenever the backing sorted set does.

Parameters:
s - the sorted set for which a dynamically typesafe view is to be returned
type - the type of element that s is permitted to hold
Returns:
a dynamically typesafe view of the specified sorted set
Since:
1.5


Related Post

Comments


©candidjava.com