i trying code snippet
map headers=new hashmap(); headers.put("x-capillary-relay","abcd"); headers.put("message-id","abcd");
now when get
either of keys working fine. seeing strange phenomenon on eclipse debugger. when debug , go inside variables , check inside table
entry @ first see this
->table --->[4] ------>key:x-capillary-relay ...........
however after debugging across 2nd line get
->table --->[4] ------>key:message-id ...........
instead of creating new entry overwrites on existing key. other key overwrite not occur. size of map shown 2. , get
works both keys. reason behind discrepancy in eclipse debugger. eclipse problem? or hashing problem. hashcode different 2 keys.
the hashcode
of keys not used is.
it applied 2 transformations (at least based on java 6 code):
static int hash(int h) { // function ensures hashcodes differ // constant multiples @ each bit position have bounded // number of collisions (approximately 8 @ default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); }
and
/** * returns index hash code h. */ static int indexfor(int h, int length) { return h & (length-1); }
since length initial capacity of hashmap (16 default), 4 both keys :
system.out.println (hash("x-capillary-relay".hashcode ())&(16-1)); system.out.println (hash("message-id".hashcode ())&(16-1));
therefore both entries stored in linked list in same bucket of map (index 4
of table
array, can see in debugger). fact debugger shows 1 of them doesn't mean other overwritten. means see key of first entry of linked list, , each new entry added head of list.
Comments
Post a Comment