With the below code, when comparing the Integer
value for a given key in a hashmap. at a borderline 126
vs 127
, the comparison returns different results. I'm wondering why will java behave this way?
Map<Character, Integer> map1 = new HashMap<>();
Map<Character, Integer> map2 = new HashMap<>();
int n = 127;
for (int i = 0; i < n; i++) {
map1.put('c', map1.getOrDefault('c', 1)+1);
map2.put('c', map2.getOrDefault('c', 1)+1);
}
System.out.println(map1.get('c') == map2.get('c')); // print true if n = 126, print false if n = 127