集合类checkedMap()方法 (Collections Class checkedMap() method)
checkedMap() Method is available in java.lang package.
CheckedMap()方法在java.lang包中可用。
checkedMap() Method is used to return the typesafe view of the given Map at runtime.
CheckedMap()方法用于在运行时返回给定Map的类型安全视图。
checkedMap() Method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
CheckedMap()方法是静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
checkedMap() Method does not throw an exception at the time of returning a validated list.
返回已验证列表时, checkedMap()方法不会引发异常。
Syntax:
句法:
public static Map checkedMap(Map map, Class key_ty, Class val_ty);
Parameter(s):
参数:
Map map – represents a map for which to get typesafe view of the given map.
地图地图 –代表要获取给定地图的类型安全视图的地图。
Class key_ty – represents the key type that the given map is allowed to store.
key_ty类 –表示允许存储给定映射的密钥类型。
Class val_ty – represents the value(val) type that the given map is allowed to store.
val_ty类 –表示允许存储给定映射的value(val)类型。
Return value:
返回值:
The return type of the method is Map, it returns typesafe view of the given map dynamically.
该方法的返回类型为Map ,它动态返回给定映射的类型安全视图。
Example:
例:
// Java Program is to demonstrate the example
// of Map checkedMap(Map map, Class key_ty, Class val_ty)
// of Collections class
import java.util.*;
public class CheckedMap {
public static void main(String args[]) {
// Create a hashmap object
HashMap < Integer, String > hm = new HashMap < Integer, String > ();
// By using put() method is to add the
// given elements in hash map
hm.put(20, "C");
hm.put(10, "C++");
hm.put(30, "JAVA");
hm.put(40, "DOTNET");
hm.put(50, "PHP");
// Display HashMap
System.out.println("link_list: " + hm);
// By using checkedMap() method is to
// represent the type safe view of the given
// Collection hashmap
Map < Integer, String > map = Collections.checkedMap(hm, Integer.class, String.class);
System.out.println();
System.out.println("Collections.checkedMap(hm, Integer.class,String.class) :");
// Display collection
System.out.println("map: " + map);
}
}
Output
输出量
link_list: {50=PHP, 20=C, 40=DOTNET, 10=C++, 30=JAVA}
Collections.checkedMap(hm, Integer.class,String.class) :
map: {50=PHP, 20=C, 40=DOTNET, 10=C++, 30=JAVA}