Home » Java programming language

Java Collections checkedSortedMap() Method with Example

Collections Class checkedSortedMap() method: Here, we are going to learn about the checkedSortedMap() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on January 07, 2020

Collections Class checkedSortedMap() method

  • checkedSortedMap() Method is available in java.lang package.
  • checkedSortedMap() Method is used to return the typesafe view of the given SortedMap at runtime.
  • checkedSortedMap() 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.
  • checkedSortedMap() Method does not throw an exception at the time of returning validated SortedMap.

Syntax:

    public static SortedMap checkedSortedMap(
        SortedMap sm, 
        Class key_ty, 
        Class val_ty
    );

Parameter(s):

  • SortedMap sm – represents the Sorted map for which to get typesafe view of the given sorted map at runtime.
  • Class key_ty – represents the key type that the given map is allowed to store.
  • Class val_ty – represents the value type that the given map is allowed to store.

Return value:

The return type of the method is SortedMap, it returns typesafe view of the given sorted map dynamically.

Example:

// Java Program is to demonstrate the example
// of SortedMap checkedSortedMap(SortedMap sm, Class key_ty, Class val_ty) 
// of Collections class

import java.util.*;

public class CheckedSortedMap {
    public static void main(String args[]) {
        // Create a sortedmap object    
        SortedMap < Integer, String > sm = new TreeMap < Integer, String > ();

        // By using put() method is to add the
        // given elements in tree map
        sm.put(20, "C");
        sm.put(10, "C++");
        sm.put(30, "JAVA");
        sm.put(40, "DOTNET");
        sm.put(50, "PHP");

        // Display SortedMap
        System.out.println("sortedmap: " + sm);

        // By using checkedSortefMap() method is to 
        // represent the type safe view of the given
        // Collection sorted map

        SortedMap < Integer, String > s_m = Collections.checkedSortedMap(sm, Integer.class, String.class);

        System.out.println();
        System.out.println("Collections.checkedSortedMap(sm, Integer.class,String.class) :");

        // Display collection
        System.out.println("sortedmap: " + s_m);
    }
}

Output

sortedmap: {10=C++, 20=C, 30=JAVA, 40=DOTNET, 50=PHP}

Collections.checkedSortedMap(sm, Integer.class,String.class) :
sortedmap: {10=C++, 20=C, 30=JAVA, 40=DOTNET, 50=PHP}



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.