当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collections checkedSortedMap()用法及代码示例


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

如果指定的映射是可序列化的,则返回的映射将是可序列化的。

由于null被认为是任何引用类型的值,因此,只要有支持映射,返回的映射就允许插入null键或值。


用法:

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

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

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

返回值:此方法返回指定映射的动态类型安全视图。

以下示例说明了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 elemnet 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 elemnet 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}


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Collections checkedSortedMap() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。