As per below code, hashmap initial dafault capacity is 16 and LF is 0.75, so when I enter 13th element then rehashing should occur and because I have provided a constant hashcode, it internally maintain a linked list to maintain an insertion order. So, till 10th elements it is working as expected but when I enter 11th element, it shuffle the order. Can anybody help me in understanding why it is happening at the time of 11th element insertion only.
class A{
int a;
A(int a){
this.a = a;
}
@Override
public int hashCode() {
return 7;
}
@Override
public String toString() {
return "" + a + "";
}
}
class Base {
public static void main(String[] args) {
Map<Object, Integer> m = new HashMap<Object, Integer>();
m.put(new A(1), 1);
m.put(new A(2), 1);
m.put(new A(3), 1);
m.put(new A(4), 1);
m.put(new A(5), 1);
m.put(new A(6), 1);
m.put(new A(7), 1);
m.put(new A(8), 1);
m.put(new A(9), 1);
m.put(new A(10), 1);
//m.put(new A(11), 1);
System.out.println(m);
}
}
Output which I am getting till 10th element:
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1}
Output which I am getting after entering 11th element:
{4=1, 1=1, 2=1, 3=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1}
It should maintain insertion order or if it is using RB tree internally so which traversal it is using here in this case?