Java Collections checkedSet() Method



Description

The Java Collections 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

Getting a TypeSafe Set from a Set of Integer Example

The following example shows the usage of Java Collection checkedSet(Set,Class ) method to get a typesafe view of set of integers. We've created a set object with some integers, printed the original set. Using checkedSet(Set, Integer) method, we're getting a set of Integer and then it is printed.

 package com.tutorialspoint; import java.util.Collections; import java.util.Set; import java.util.TreeSet; public class CollectionsDemo { public static void main(String[] args) { Set<Integer> set = new TreeSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); System.out.println("Initial collection value: " + set); Set<Integer> safeSet = Collections.checkedSet(set, Integer.class); System.out.println("Typesafe View: "+safeSet); } } 

Output

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

 Initial collection value: [1, 2, 3, 4, 5] Typesafe View: [1, 2, 3, 4, 5] 

Getting a TypeSafe Set from a Set of String Example

The following example shows the usage of Java Collection checkedSet(Set,Class ) method to get a typesafe view of set of strings. We've created a set object with some integers, printed the original set. Using checkedSet(Set, String) method, we're getting a set of String and then it is printed.

 package com.tutorialspoint; import java.util.Collections; import java.util.Set; import java.util.TreeSet; public class CollectionsDemo { public static void main(String[] args) { Set<String> set = new TreeSet<>(); set.add("Welcome"); set.add("to"); set.add("Tutorialspoint"); System.out.println("Initial collection value: " + set); Set<String> safeSet = Collections.checkedSet(set, String.class); System.out.println("Typesafe View: "+safeSet); } } 

Output

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

 Initial collection value: [Tutorialspoint, Welcome, to] Typesafe View: [Tutorialspoint, Welcome, to] 

Getting a TypeSafe Set from a Set of Objects Example

The following example shows the usage of Java Collection checkedSet(Set,Class ) method to get a typesafe view of set of Student objects. We've created a Set object with some student objects, printed the original set. Using checkedSet(Set, Student) method, we're getting a Set of Students and then it is printed.

 package com.tutorialspoint; import java.util.Collections; import java.util.Set; import java.util.TreeSet; public class CollectionsDemo { public static void main(String[] args) { Set<Student> set = new TreeSet<>(); set.add(new Student(1, "Julie")); set.add(new Student(2, "Robert")); set.add(new Student(3, "Adam")); System.out.println("Initial collection value: " + set); Set<Student> safeSet = Collections.checkedSet(set, Student.class); System.out.println("Typesafe View: "+safeSet); } } class Student implements Comparable<Student> { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public int compareTo(Student student) { return student.rollNo - this.rollNo; } } 

Output

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

 Initial collection value: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]] Typesafe View: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]] 
java_util_collections.htm
Advertisements