Collections checkedSet() method in Java with Examples

Last Updated : 3 Aug, 2026

The Collections.checkedSet() method returns a dynamically type-safe view of the specified Set. It ensures that only elements of the specified type can be added to the set at runtime. If an element of an incorrect type is inserted, a ClassCastException is thrown immediately.

  • Throws ClassCastException for invalid element types.
  • Changes made through the returned view are reflected in the original set.
Java
import java.util.*;

public class GFG {
    public static void main(String[] args) {

        Set<String> names = new HashSet<>();
        names.add("Alice");
        names.add("Bob");

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

        checkedSet.add("Charlie");

        System.out.println(checkedSet);
    }
}

Output
[Bob, Alice, Charlie]

Explanation: The checkedSet() method creates a runtime type-safe view of the original set. Since only String values are added, no exception occurs.

Syntax

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

Return Value: Returns a dynamically type-safe view of the specified set.

Parameters:

  • s: The set for which a type-safe view is to be created.
  • type: The Class object representing the type of elements that the set is allowed to store.

Exception: Throws ClassCastException if an element of the wrong type is inserted into the checked set.

Example: Creating a Type-Safe String Set

Java
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]

Explanation: This example creates a TreeSet<String> and wraps it using Collections.checkedSet(). The returned set accepts only String elements and prevents insertion of objects of any other type.

Example: Creating a Type-Safe Integer Set

Java
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]

Explanation: This example creates a TreeSet<Integer> and obtains a type-safe view using Collections.checkedSet(). Only Integer values can be added through the returned set.

Advantages

  • Ensures runtime type safety for sets.
  • Detects invalid element insertion immediately.
  • Helps prevent ClassCastException later in the program.
  • Useful when working with legacy or mixed-type collections.
Comment