Java 集合 checkedNavigableMap() 方法



描述

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

宣告

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

public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K,V> m,Class<K> keyType,Class<V> valueType)

引數

  • m − 這是要返回動態型別安全檢視的可導航對映。

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

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

返回值

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

異常

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

以下示例演示了 Java Collection checkedNavigableMap(NavigableMap,Class,Class) 方法的使用,以獲取字串和整數對映的型別安全檢視。我們建立了一個包含一些字串和整數的 Map 物件。使用 checkedNavigableMap(NavigableMap, String, Integer) 方法,我們獲得了字串和整數的對映,然後將其打印出來。

package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create map     
      NavigableMap<String,Integer> hmap = new TreeMap<>();

      // 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
      NavigableMap<String,Integer> tsmap;
      tsmap = Collections.checkedNavigableMap(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 Collection checkedNavigableMap(NavigableMap,Class,Class) 方法的使用,以獲取字串和字串對映的型別安全檢視。我們建立了一個包含一些字串和字串的 Map 物件。使用 checkedNavigableMap(NavigableMap, String, String) 方法,我們獲得了字串和字串的對映,然後將其打印出來。

package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create map     
      NavigableMap<String,String> hmap = new TreeMap<>();

      // 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
      NavigableMap<String,String> tsmap;
      tsmap = Collections.checkedNavigableMap(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 Collection checkedCollection(NavigableMap,Class) 方法的使用,以獲取 Student 物件對映的型別安全檢視。我們建立了一個包含一些 Student 物件的對映,並列印了原始對映。使用 checkedCollection(NavigableMap, Student) 方法,我們獲得了字串的可導航對映,然後將其打印出來。

package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class CollectionsDemo {

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

      // 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
      NavigableMap<String,Student> tsmap;
      tsmap = Collections.checkedNavigableMap(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
廣告
© . All rights reserved.