I have written below code to test the behavior of hashmap when all elements are ended in the same bucket:-
public class DerivedMain {
int data = 10;
@Override
public int hashCode() {
return data;
}
public static void main(String[] args) {
HashMap m = new HashMap();
for(int i=0;i<20;i++) {
m.put(i, i);
}
Field tableField = null;
try {
tableField = HashMap.class.getDeclaredField("table");
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tableField.setAccessible(true);
Object[] table = null;
try {
table = (Object[]) tableField.get(m);
} catch (IllegalArgumentException | IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(table == null ? 0 : table.length);
}
}
I got the following output:- 32
Why is resizing happening even when all the elements are ending in same bucket?
Aucun commentaire:
Enregistrer un commentaire