Java 集合 checkedMap() 方法



描述

checkedMap(Map<, V>, Class<K>, Class<V>) 方法用於獲取指定對映的動態型別安全檢視。

宣告

以下是 java.util.Collections.checkedMap() 方法的宣告。

public static <K,V> Map<K,V> checkedMap(Map<K,V> m,Class<K> keyType,Class<V> valueType)

引數

  • m - 這是要返回其動態型別安全檢視的對映。

  • keyType - 這是 m 允許儲存的鍵的型別。

  • valueType - 這是 m 允許儲存的值的型別。

返回值

方法呼叫返回指定對映的動態型別安全檢視。

異常

從字串、整數對對映獲取型別安全對映示例

以下示例演示瞭如何使用 Java 集合 checkedMap(Map,Class,Class) 方法獲取字串和整數對映的型別安全檢視。我們建立了一個包含一些字串和整數的對映物件。使用 checkedMap(Map, String, Integer) 方法,我們獲取了一個字串、整數對映,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CollectionsDemo {
   public static void main(String args[]) {
   
      // create map     
      HashMap<String,Integer> hmap = new HashMap<>();

      // populate the map
      hmap.put("1", 1);
      hmap.put("2", 2);
      hmap.put("3", 3);
      hmap.put("4", 4);

      // get typesafe view of the map
      Map<String,Integer> tsmap;
      tsmap = Collections.checkedMap(hmap,String.class,Integer.class);     

      System.out.println("Dynamically typesafe view of the map: "+tsmap);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Dynamically typesafe view of the map: {1=1, 2=2, 3=3, 4=4}

從字串、字串對對映獲取型別安全對映示例

以下示例演示瞭如何使用 Java 集合 checkedMap(Map,Class,Class) 方法獲取字串和字串對映的型別安全檢視。我們建立了一個包含一些字串和字串的對映物件。使用 checkedMap(Map, String, String) 方法,我們獲取了一個字串、字串對映,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CollectionsDemo {
   public static void main(String args[]) {
   
      // create map     
      HashMap<String,String> hmap = new HashMap<>();

      // populate the map
      hmap.put("1", "A");
      hmap.put("2", "B");
      hmap.put("3", "C");
      hmap.put("4", "D");

      // get typesafe view of the map
      Map<String,String> tsmap;
      tsmap = Collections.checkedMap(hmap,String.class,String.class);     

      System.out.println("Dynamically typesafe view of the map: "+tsmap);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Dynamically typesafe view of the map: {1=A, 2=B, 3=C, 4=D}

從字串、物件對對映獲取型別安全對映示例

以下示例演示瞭如何使用 Java 集合 checkedCollection(Map,Class) 方法獲取學生物件對映的型別安全檢視。我們建立了一個包含一些學生物件的對映,並列印了原始對映。使用 checkedCollection(Map, Student) 方法,我們獲取了一個字串對映,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CollectionsDemo {

   public static void main(String[] args) {
   
      // create map     
      HashMap<String,Student> hmap = new HashMap<>();

      // populate the map
      hmap.put("1", new Student(1, "Julie"));
      hmap.put("2", new Student(2, "Robert"));
      hmap.put("3", new Student(3, "Adam"));
      hmap.put("4", new Student(4, "Jene"));

      // get typesafe view of the map
      Map<String,Student> tsmap;
      tsmap = Collections.checkedMap(hmap,String.class,Student.class);     

      System.out.println("Dynamically typesafe view of the map: "+tsmap);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Dynamically typesafe view of the map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ], 4=[ 4, Jene ]}
java_util_collections.htm
廣告