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]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.