import java.util.*;
public class MyClass {
public static void main(String args[]) {
Map<Float, Integer> m = new HashMap<>();
m.put(Float.intBitsToFloat(0x7f800001), 1);
m.put(Float.intBitsToFloat(0x7f800002), 2);
System.out.println(m.size());
}
}
为什么上面的代码返回1
作为m
的大小? 0x7f800001
和 0x7f800002
都是 NaN 浮点值,但由于 NaN != NaN
根据定义,这不应导致冲突。
该行为类似于 Java Hashmap 中记录的对 null
键的处理,但我找不到任何表明 NaN
被处理为 的内容由 HashMap 为 null
。
最佳答案
Float.equals(Object)
记录为:
Compares this object against the specified object. The result is true if and only if the argument is not null and is a
Float
object that represents a float with the same value as the float represented by this object. For this purpose, two float values are considered to be the same if and only if the methodfloatToIntBits(float)
returns the identicalint
value when applied to each.
现在听起来不同的 NaN 值应该被视为不相等,但是 floatToIntBits
文档包括(强调我的):
If the argument is NaN, the result is 0x7fc00000.
In all cases, the result is an integer that, when given to the
intBitsToFloat(int)
method, will produce a floating-point value the same as the argument tofloatToIntBits
(except all NaN values are collapsed to a single "canonical" NaN value).
所以基本上,Float.equals
(和 Float.hashCode
,它也使用 floatToIntBits(float)
)将所有 NaN 值视为相等.
关于java - 为什么 HashMap 不能有不同的 NaN 值作为键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73647347/