Home » 
        Java programming language
    
    Java Collections checkedSet() Method with Example
    
    
    
            
        Collections Class checkedSet() method: Here, we are going to learn about the checkedSet() method of Collections Class with its syntax and example.
        Submitted by Preeti Jain, on January 07, 2020
    
    
    Collections Class checkedSet() method
    
        - checkedSet() Method is available in java.lang package.
 
        - checkedSet() Method is used to returns the typesafe view of the given set at runtime.
 
        - checkedSet() Method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
 
        - checkedSet() Method does not throw an exception at the time of returning validated set.
 
    
    
Syntax:
    public static Set checkedSet(Set set, Class ele_ty);
    Parameter(s):
    
        - Set set – represents the set for which to get typesafe view of the given Set(set).
 
        - Class ele_ty – represents the type of elements (ele_ty) that the given set is allowed to store.
 
    
    
    Return value:
    The return type of the method is Set, it returns typesafe view of the given set dynamically.
    
    Example:
// Java Program is to demonstrate the example
// of Set checkedSet(Set set, Class ele_ty) of Collections class
import java.util.*;
public class CheckedSet {
    public static void main(String args[]) {
        // Create a hashset object    
        HashSet < Integer > hs = new HashSet < Integer > ();
        // By using ad() method is to add the
        // given elements in hash set
        hs.add(20);
        hs.add(10);
        hs.add(30);
        hs.add(40);
        hs.add(50);
        // Display HashSet
        System.out.println("hashset: " + hs);
        // By using checkedSet() method is to 
        // represent the type safe view of the given
        // Collection hashset
        Set < Integer > set = Collections.checkedSet(hs, Integer.class);
        System.out.println();
        System.out.println("Collections.checkedSet(hs, Integer.class) :");
        // Display collection
        System.out.println("set : " + set);
    }
}
Output
hashset: [50, 20, 40, 10, 30]
Collections.checkedSet(hs, Integer.class) :
set : [50, 20, 40, 10, 30]
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement