Java Collection Tutorial - Java Collections.checkedSet(Set <E> s, Class <E> type)








Syntax

Collections.checkedSet(Set <E> s, Class <E> type) has the following syntax.

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

Example

In the following code shows how to use Collections.checkedSet(Set <E> s, Class <E> type) method.

/*from  ww w.java2  s  .  c  om*/
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class Main {
   public static void main(String args[]) {
   
      Set<String>  hset = new TreeSet<String> ();
      
      // populate the set
      hset.add("from");
      hset.add("java2s.com");
      hset.add("tutorial");
      
      // get typesafe view of the set
      Set<String>  tsset = Collections.checkedSet(hset,String.class);     
      
      System.out.println(tsset);
   }    
}

The code above generates the following result.