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