java.util.Collections.checkedSet() Method



Description

The checkedSet(Set<E>, Class<E>) method is used to get a dynamically typesafe view of the specified set.

Declaration

Following is the declaration for java.util.Collections.checkedSet() method.

public static <E> Set<E> checkedSet(Set<E> s, Class<E> type)

Parameters

  • s − This is the set for which a dynamically typesafe view is to be returned.

  • type −- This is the type of element that s is permitted to hold.

Return Value

The method call returns a dynamically typesafe view of the specified set.

Exception

NA

Example

The following example shows the usage of java.util.Collections.checkedSet()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   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);
   }    
}

Let us compile and run the above program, this will produce the following result.

Dynamically typesafe view of the set: [Collections, in, java]
java_util_collections.htm
Advertisements