5:50 am - Thursday August 17, 2017

Program for creating binary search checked set display values in list of data using checkedSet(Set s, Class type)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Program for creating binary search checked set display values in list of data.

Example Program

 


import java.util.*;

public class CollectionsDemo1 {

	public static void main(String args[]) {

		// create set

		TreeSet<String> hset = new TreeSet<String>();

		// populate the set

		hset.add("Collections");

		hset.add("in");

		hset.add("java");

		// get typesafe view of the set

		Set<String> tsset = Collections.checkedSet(hset, String.class);

		System.out.println("Dynamically typesafe view of the set: " + tsset);

	}

}

 

Output

Dynamically typesafe view of the set: [Collections, in, java]

 

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

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.