Consider the following code snippet :
public class MainClass {
public static void main(String[] arg) {
Map<String, Object> map = new HashMap<>();
map.put("ONE", new BigInteger("1"));
map.put("TWO", new BigInteger("2"));
map.put("THREE", new BigInteger("3"));
map.put("FOUR", new BigInteger("4"));
map.put("FIVE", new BigInteger("5"));
map.put("SIX", new BigInteger("6"));
System.out.println("Hello !");
}
The result is:
- Map's
size
is 6. Map's
table
contains the following content:[FIVE=5, SIX=6, ONE=1, TWO=2, THREE=3]
FOUR
disappeared. As a comment stated before, it would be wise to calculate the hashCode
of my entries, modulo the size of the map. It gives the following result:
ONE : 0
TWO : 0
THREE : 1
FOUR : 2
FIVE : 3
SIX : 4
As we can see, the hashCode
does not indicate anything considering the collision of FOUR
with any value. This also works with ConcurrentHashMap
and LinkedHashMap
, so I guess it is a HashMap
problem.
Can someone explain to me what is actually going on ? I'm quite lost on this one.
I'm working with:
- Eclipse Neon
- JDK 8