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());
}
}
Why does the above code return 1 as the size of m? 0x7f800001 and 0x7f800002 are both NaN float values, but since NaN != NaN by definition, this should not cause a collision.
The behavior is similar to the documented handling of null keys in the Java Hashmap, but I can't find anything that indicates that NaN is handled as null by HashMap.