I'm new to using hash table structures. I'm using LinkedHashMap (ex: cache = new LinkedHashMap<K,V>(...)) to implement my own cache. I have a list of questions about this data structure:
I set a parameter capacity = 100 (eg.), it means that number of items in bucket is limited to 100. Then if I insert a new item into this cache (when cache size = 100), am I correct in thinking the evict policy will happen?
In my implementation, keys are composite object include two items like this:
class Key { public string a; public string b; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((a == null) ? 0 : a.hashCode()); result = prime * result + ((b == null) ? 0 : b.hashCode()); return result; } }With this
hashcode(), suppose the bucket already has 100 items. When I insert a new item, assuming thathashcode()returns a duplicate key with a previous item, my understanding is that linkedhashmap will remove the eldest item using the evict policy and use linkedlist to handle collision for the new item, so the number of items in the bucket will be 99. Is it right ?Is there any way to identify which entries in the bucket current contain a chain for handle collision?
Keyimmutable if you are using it as a hash-based map key: make classfinal, and makeaandbfinal.hashCodeimplementation is the same as simplyreturn Objects.hash(a, b);.