关于Map英文文档的一段翻译

本文探讨了Java集合框架中Map接口的containsKey方法的实现原理,并深入分析了equals方法在此过程中的作用及其可能的优化手段。

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

//好难翻译啊

Many methods in Collections Framework interfaces are defined in terms of the equals method. For example, the specification for the containsKey(Object key) method says: "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))." This specification should not be construed to imply that invoking Map.containsKey with a non-null argument key will cause key.equals(k) to be invoked for any key k. Implementations are free to implement optimizations whereby the equals invocation is avoided, for example, by first comparing the hash codes of the two keys. (The Object.hashCode() specification guarantees that two objects with unequal hash codes cannot be equal.) More generally, implementations of the various Collections Framework interfaces are free to take advantage of the specified behavior of underlying Object methods wherever the implementor deems it appropriate

集合框架接口中许多方法是被定义的(但如何定义看个人行为(自己补充的)),拿equals方法来说,我们可以自己去定义它。我们还可以举个例子,containsKey(Object key)方法的定义是说“返回为true,如果并且只要这个这个map包含了键值为k,例如((key==null ? k==null : key.equals(k))。”,这个定义不应该解释说明为调用非空参数的keyMap.containsKey方法将会导致key.equals(k)会被调用对于任何键值k。实现(Implementations)是自由地去实现优化即你可以不用调用equals方法来实现。例如,首先可以通过比较两个键值的hash码(解释不多说)。通常情况下,实现者只要认为它是合适的,不同集合框架接口的实现是自由利用底层对象方法的特定行为的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值