📜  Java中的集合checkedSortedMap()方法和示例

📅  最后修改于: 2022-05-13 01:54:55.312000             🧑  作者: Mango

Java中的集合checkedSortedMap()方法和示例

Java.util.Collections类的checkedSortedMap()方法用于返回指定排序映射的动态类型安全视图。
如果指定的映射是可序列化的,则返回的映射将是可序列化的。
由于 null 被认为是任何引用类型的值,因此返回的映射允许在支持映射时插入空键或值。
句法:

public static  SortedMap
 checkedSortedMap(SortedMap m, Class keyType, Class valueType)

参数:此方法将以下参数作为参数

  • m –要为其返回动态类型安全视图的映射
  • keyType –允许 m 持有的密钥类型
  • valueType –允许 m 持有的值的类型

返回值:此方法返回指定映射的动态类型安全视图
以下是说明checkedSortedMap()方法的示例
示例 1:

Java
// Java program to demonstrate
// checkedSortedMap() method
// for  type
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // creating object of SortedMap
            SortedMap
                smap = new TreeMap();
 
            // Adding element to smap
            smap.put("Ram", "Gopal");
            smap.put("Karan", "Arjun");
            smap.put("Karn", "Veer");
 
            // printing the sorted map
            System.out.println("Sorted map:\n"
                               + smap);
 
            // create typesafe view of the specified map
            SortedMap
                tsmap = Collections
                            .checkedSortedMap(smap,
 
Output:
{Karan=39, Karn=40, Ram=20}
                                              String.class,                                               String.class);               // printing the typesafe view of specified sorted map             System.out.println("Typesafe view of sorted map:\n"                                + tsmap);         }           catch (IllegalArgumentException e) {             System.out.println("Exception thrown : " + e);         }     } }


Java
// Java program to demonstrate
// checkedSortedMap() method
// for  type
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of SortedMap
            SortedMap smap = new TreeMap();
 
            // Adding element to smap
            smap.put("Ram", 20);
            smap.put("Karan", 39);
            smap.put("Karn", 40);
            // printing the sorted map
            System.out.println("Sorted map:\n"
                               + smap);
 
            // create typesafe view of the specified map
            SortedMap
                tsmap = Collections
                            .checkedSortedMap(smap,
                                              String.class,
                                              Integer.class);
 
            // printing the typesafe view of specified sorted map
            System.out.println("Typesafe view of sorted map:\n"
                               + tsmap);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
Sorted map:
{Karan=Arjun, Karn=Veer, Ram=Gopal}
Typesafe view of sorted map:
{Karan=Arjun, Karn=Veer, Ram=Gopal}

示例 2:

Java

// Java program to demonstrate
// checkedSortedMap() method
// for  type
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of SortedMap
            SortedMap smap = new TreeMap();
 
            // Adding element to smap
            smap.put("Ram", 20);
            smap.put("Karan", 39);
            smap.put("Karn", 40);
            // printing the sorted map
            System.out.println("Sorted map:\n"
                               + smap);
 
            // create typesafe view of the specified map
            SortedMap
                tsmap = Collections
                            .checkedSortedMap(smap,
                                              String.class,
                                              Integer.class);
 
            // printing the typesafe view of specified sorted map
            System.out.println("Typesafe view of sorted map:\n"
                               + tsmap);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Sorted map:
{Karan=39, Karn=40, Ram=20}
Typesafe view of sorted map:
{Karan=39, Karn=40, Ram=20}