Created
December 30, 2017 08:44
-
-
Save jnizet/34ca08ba0314c8e857ea9a161c175f13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.util.HashMap; | |
public class MapTest { | |
private static class X { | |
private int value; | |
public X(int value) { | |
this.value = value; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
X x = (X) o; | |
return value == x.value; | |
} | |
@Override | |
public int hashCode() { | |
return 1; | |
} | |
} | |
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { | |
HashMap<Integer, Integer> map = new HashMap<>(); | |
for (int i = 0; i < 16; i++) { | |
printMap(map); | |
map.put(i, i); | |
} | |
System.out.println(); | |
HashMap<X, Integer> map2 = new HashMap<>(); | |
for (int i = 0; i < 16; i++) { | |
printMap(map2); | |
map2.put(new X(i), i); | |
} | |
} | |
private static void printMap(HashMap<?, ?> map) throws NoSuchFieldException, IllegalAccessException { | |
System.out.println("map.size() = " + map.size()); | |
Field internalTable = HashMap.class.getDeclaredField("table"); | |
internalTable.setAccessible(true); | |
Object[] table = (Object[]) internalTable.get(map); | |
if (table == null) { | |
System.out.println("table = " + null); | |
} | |
else { | |
System.out.println("table.length = " + table.length); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment