Java中的集合checkedMap()方法和示例
Collections 类的checkedMap()方法存在于Java.util 包中,用于返回指定映射的动态类型安全视图。如果指定的映射是可序列化的,则返回的映射将是可序列化的。由于 null 被认为是任何引用类型的值,因此返回的映射允许在支持映射时插入空键或值。
句法:
public static Map
checkedMap(Map m, Class keyType, Class valueType)
参数:此方法采用以下 3 个参数作为参数,如下所列:
- 要为其返回动态类型安全视图的映射
- 允许 m 持有的密钥类型
- m 被允许持有的值的类型
返回值:此方法返回指定映射的动态类型安全视图。
异常:此方法确实抛出 ClassCastException
Tip: This method is compatible with java version 1.5 and onwards.
现在让我们看几个例子,以实现上述方法并更好地理解该方法,如下所示:
示例 1:
Java
// Java program to Demonstrate checkedMap() method
// of Collections class
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating an empty HashMap by declaring
// HashMap key-value pairs
// of string and string type
HashMap hmap
= new HashMap();
// Adding custom key-value pairs to above
// HashMap
hmap.put("Ram", "Shyam");
hmap.put("Karan", "Arjun");
hmap.put("Karn", "Veer");
hmap.put("duryodhan", "dhrupat");
// Printing all the key-value pairs of above
// HashMap
System.out.println("Map: \n" + hmap);
// Now creating typesafe view of the specified
// map using checkedMap() method of Collections
// class
Map tsmap
= Collections.checkedMap(hmap, String.class,
String.class);
// Now printing the typesafe view of specified
// list
System.out.println("Typesafe view of Map: "
+ tsmap);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message when exception occurs
System.out.println("Exception thrown : \n" + e);
}
}
}
Java
// Java program to demonstrate checkedMap() method
// of Collections class
// Importing all required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating an empty HashMap by
// declaring object of string and integer type
HashMap hmap
= new HashMap();
// Adding key-value pairs to above HashMap
// object
hmap.put("Player-1", 20);
hmap.put("Player-2", 30);
hmap.put("Player-3", 40);
// Printing the elements inside above HashMap
// object
System.out.println("Map: \n" + hmap);
// Now creating typesafe view of the specified
// map using checkedMap() method
Map tsmap
= Collections.checkedMap(hmap, String.class,
Integer.class);
// Again printing the typesafe view of specified
// list
System.out.println("Typesafe view of Map: \n"
+ tsmap);
}
// Catch block to handle exceptions
catch (IllegalArgumentException e) {
// Display message when exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Map:
{Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}
Typesafe view of Map:
{Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}
示例 2:
Java
// Java program to demonstrate checkedMap() method
// of Collections class
// Importing all required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating an empty HashMap by
// declaring object of string and integer type
HashMap hmap
= new HashMap();
// Adding key-value pairs to above HashMap
// object
hmap.put("Player-1", 20);
hmap.put("Player-2", 30);
hmap.put("Player-3", 40);
// Printing the elements inside above HashMap
// object
System.out.println("Map: \n" + hmap);
// Now creating typesafe view of the specified
// map using checkedMap() method
Map tsmap
= Collections.checkedMap(hmap, String.class,
Integer.class);
// Again printing the typesafe view of specified
// list
System.out.println("Typesafe view of Map: \n"
+ tsmap);
}
// Catch block to handle exceptions
catch (IllegalArgumentException e) {
// Display message when exception occurs
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}