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}
相关用法
- Java Collections max()用法及代码示例
- Java Collections min()用法及代码示例
- Java Collections asLifoQueue()用法及代码示例
- Java Collections synchronizedCollection()用法及代码示例
- Java Collections synchronizedList()用法及代码示例
- Java Collections swap()用法及代码示例
- Java Collections unmodifiableCollection()用法及代码示例
- Java Collections list()用法及代码示例
- Java Collections newSetFromMap()用法及代码示例
- Java Collections replaceAll()用法及代码示例
- Java Collections singletonMap()用法及代码示例
- Java Collections singletonList()用法及代码示例
- Java Collections synchronizedSortedSet()用法及代码示例
- Java Collections synchronizedMap()用法及代码示例
- Java Collections synchronizedSet()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Collections checkedMap() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。