Java Collections CheckMap()方法与示例

本文详细介绍了Java中Collections类的checkedMap()方法,该方法在java.lang包中,用于在运行时返回给定Map的类型安全视图。通过实例演示了如何使用此方法创建类型安全的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

集合类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}


翻译自: https://www.includehelp.com/java/collections-checkedmap-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值