Wednesday, July 3, 2024
HomeLanguagesJavaCollections checkedSet() method in Java with Examples

Collections checkedSet() method in Java with Examples

The checkedSet() method of java.util.Collections class is used to return a dynamically typesafe view of the specified set.
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.
Syntax: 
 

public static  Set checkedSet(Set s, Class type)

Parameters: This method takes the following argument as a parameter 
 

  • s: the set for which a dynamically typesafe view is to be returned
  • type: the type of element that s is permitted to hold

Return Value: This method returns a dynamically typesafe view of the specified set.
Below are the examples to illustrate the checkedSet() method
Example 1: 
 

Java




// Java program to demonstrate
// checkedSet() method
// for String value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // creating object of Set<String>
            Set<String> hset = new TreeSet<String>();
 
            // Adding element to hmap
            hset.add("Ram");
            hset.add("Gopal");
            hset.add("Verma");
 
            // print the set
            System.out.println("Set: " + hset);
 
            // create typesafe view of the specified set
            Set<String>
                tsset = Collections
                            .checkedSet(hset, String.class);
 
            // printing the typesafe view of specified list
            System.out.println("Typesafe view of Set: "
                               + tsset);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Set: [Gopal, Ram, Verma]
Typesafe view of Set: [Gopal, Ram, Verma]

 

Example 2: 
 

Java




// Java program to demonstrate
// checkedSet() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of Set<Integer>
            Set<Integer> hset = new TreeSet<Integer>();
 
            // Adding element to hset
            hset.add(20);
            hset.add(30);
            hset.add(40);
 
            // print the set
            System.out.println("Set: " + hset);
 
            // create typesafe view of the specified set
            Set<Integer>
                tsset = Collections
                            .checkedSet(hset, Integer.class);
 
            // printing the typesafe view of specified list
            System.out.println("Typesafe view of Set: " + tsset);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Set: [20, 30, 40]
Typesafe view of Set: [20, 30, 40]

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments