Java Collections checkedSortedMap()方法及示例

Java Collections checkedSortedMap()方法及示例

java.util.Collections 类的 checkedSortedMap() 方法用于返回指定排序Map的动态类型安全视图。

如果指定的Map是可序列化的,返回的Map将是可序列化的。

因为null被认为是任何参考类型的值,所以只要支持的Map允许插入null键或值。

语法:

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

参数: 该方法需要以下参数作为参数

  • m – 要返回动态类型安全视图的Map
  • keyType – m允许持有的键的类型
  • valueType - m允许持有的值的类型。

返回值: 该方法返回指定Map的动态 类型安全视图
以下是说明checkedSortedMap()方法的例子

例1:

// Java program to demonstrate
// checkedSortedMap() method
// for <String, String> type
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // creating object of SortedMap<String, String>
            SortedMap<String, String>
                smap = new TreeMap<String, String>();
 
            // 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<String, String>
                tsmap = Collections
                            .checkedSortedMap(smap,
 
<strong>Output:</strong>
<pre>{Karan=39, Karn=40, Ram=20}</pre>
                                              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);
        }
    }
}

输出

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

例2:

// Java program to demonstrate
// checkedSortedMap() method
// for <String, Integer> type
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of SortedMap<String, Integer>
            SortedMap<String, Integer> smap = new TreeMap<String, Integer>();
 
            // 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<String, Integer>
                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}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程