Java Collections checkedNavigableMap() Method



Description

The checkedNavigableMap(NavigableMap<, V>, Class<K>, Class<V>) method is used to get a dynamically typesafe view of the specified map.

Declaration

Following is the declaration for java.util.Collections.checkedNavigableMap() method.

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

Parameters

  • m − This is the navigable map for which a dynamically typesafe view is to be returned.

  • keyType − This is the type of key that m is permitted to hold.

  • valueType − This is the type of value that m is permitted to hold.

Return Value

The method call returns a dynamically typesafe view of the specified navigable map.

Exception

NA

Getting a TypeSafe Navigable Map from a Map of String, Integer pair Example

The following example shows the usage of Java Collection checkedNavigableMap(NavigableMap,Class,Class ) method to get a typesafe view of map of String and Integers. We've created a Map object with some string and integers. Using checkedNavigableMap(NavigableMap, String, Integer) method, we're getting a map of String, Integer and then it is printed.

 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); } } 

Output

Let us compile and run the above program, this will produce the following result.

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

Getting a TypeSafe Navigable Map from a Map of String, String pair Example

The following example shows the usage of Java Collection checkedNavigableMap(NavigableMap,Class,Class ) method to get a typesafe view of map of String and Strings. We've created a Map object with some string and strings. Using checkedNavigableMap(NavigableMap, String, String) method, we're getting a map of String, String and then it is printed.

 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); } } 

Output

Let us compile and run the above program, this will produce the following result.

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

Getting a TypeSafe Navigable Map from a Map of String, Object pair Example

The following example shows the usage of Java Collection checkedCollection(NavigableMap,Class ) method to get a typesafe view of map of Student objects. We've created a map with some student objects, printed the original map. Using checkedCollection(NavigableMap, Student) method, we're getting a navigable map of String and then it is printed.

 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 + " ]"; } } 

Output

Let us compile and run the above program, this will produce the following result.

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