當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Collections checkedMap()用法及代碼示例


java.util.Collections類的checkedMap()方法用於返回指定映射的動態類型安全視圖。

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

由於null被認為是任何引用類型的值,因此,隻要有支持映射,返回的映射就允許插入null鍵或值。


用法:

public static  Map 
    checkedMap(Map m, Class keyType, Class valueType)

參數:此方法將以下參數作為參數:

  • m –要為其返回動態類型安全視圖的Map
  • keyType –m允許持有的 key 的類型
  • valueType –m允許持有的值的類型

返回值:此方法返回指定映射的動態類型安全視圖。

以下示例說明了checkedMap()方法

示例1:

// Java program to demonstrate 
// checkedMap() method 
// for <String, String> value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
  
        try { 
  
            // creating object of HashMap<String> 
            HashMap<String, String> 
                hmap = new HashMap<String, String>(); 
  
            // Adding elemnet to hmap 
            hmap.put("Ram", "Shyam"); 
            hmap.put("Karan", "Arjun"); 
            hmap.put("Karn", "Veer"); 
            hmap.put("duryodhan", "dhrupat"); 
  
            // printing the map 
            System.out.println("Map: \n" + hmap); 
  
            // create typesafe view of the specified map 
            // using checkedMap() method 
            Map<String, String> 
                tsmap = Collections 
                            .checkedMap(hmap, 
                                        String.class, 
                                        String.class); 
  
            // printing the typesafe view of specified list 
            System.out.println("Typesafe view of Map: " + tsmap); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown : \n" + e); 
        } 
    } 
}
輸出:
Map: 
{Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}
Typesafe view of Map: 
{Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}

示例2:

// Java program to demonstrate 
// checkedMap() method 
// for <String, Integer> value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
  
            // creating object of HashMap<String> 
            HashMap<String, Integer> 
                hmap = new HashMap<String, Integer>(); 
  
            // Adding elemnet to hmap 
            hmap.put("Player-1", 20); 
            hmap.put("Player-2", 30); 
            hmap.put("Player-3", 40); 
  
            // printing the map 
            System.out.println("Map: \n" + hmap); 
  
            // create typesafe view of the specified map 
            // using checkedMap() method 
            Map<String, Integer> 
                tsmap = Collections 
                            .checkedMap(hmap, String.class, Integer.class); 
  
            // printing the typesafe view of specified list 
            System.out.println("Typesafe view of Map: \n" + tsmap); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Map: 
{Player-1=20, Player-3=40, Player-2=30}
Typesafe view of Map: 
{Player-1=20, Player-3=40, Player-2=30}


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections checkedMap() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。