Collections public static Set checkedSet(Set s, Class type) method Example Program


Returns a dynamically typesafe view of the specified set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a set contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all subsequent access to the set takes place through the view, it is guaranteed that the 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 set will be serializable if the specified set is serializable.
Since null is considered to be a value of any reference type, the returned set permits insertion of null elements whenever the backing set does.

Program

package com.candidjava;

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

/**
 * 
 * @author karthikeyan.T
 * @description the following code shows how to use Collections.checkedSet(Set
 *              <E> s, Class <E> type) method.
 */
	public class CheckedSet {
		public static void main(String args[]) {
			Set<String>  hset = new TreeSet<String> ();
			hset.add("karthik");
			hset.add("mohan");
			hset.add("kamal");
			hset.add("hari");
			hset.add("vinodh");
			hset.add("anand");
			Set<String>  tset = Collections.checkedSet(hset,String.class);
			System.out.println(tset);
		}
	}


Output

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

Explanation

public static <E> Set<E> checkedSet(Set<E> s,Class<E> type)
Returns a dynamically typesafe view of the specified set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a set contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all subsequent access to the set takes place through the view, it is guaranteed that the 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 set will be serializable if the specified set is serializable.
Since null is considered to be a value of any reference type, the returned set permits insertion of null elements whenever the backing set does.

Parameters:
s - the 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 set
Since:
1.5


Related Post

Comments


©candidjava.com