R
Red Orchid
The 'HashMap.put' has the code "if (e.hash == hash && eq(k, e.key))".
Do you think that the 'e.hash == hash' is required ?
I think that the "if (k == e.key || k.equals(e.key))" is proper.
For further details,
the following codes are parts of 'HashMap'.
<code>
public V put(K key, V value) {
K k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table; e != null; e = e.next) {
if (e.hash == hash && eq(k, e.key)) { // #1
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
...
static int indexFor(int h, int length) {
return h & (length-1); // #2
}
static boolean eq(Object x, Object y) {
return x == y || x.equals(y); // #3
}
</code>
The linked list of the 'table' is heterogeneous with
hash value. That is, when hash value of 'E0' is 5
and one of 'E1' is 6, both 'E0' and 'E1' can exist in
the same bucket 'table' because of the above #2
( 00 & 10 = 00, 01 & 10 = 00).
But, it do not justify the code "if (e.hash == hash" of #1.
Because, ...
a) The functions of 'Map' puts/gets the value 'V' that is
mapped by the key 'K', not by the hash value.
b) The execution time of "k == e.key" by #3 is not large
than one of 'e.hash == hash'.
Therefore, I think,
The code "if (k == e.key || k.equals(e.key))" is proper
instead of #1. #1 is overabundance.
I think that if there is overabundance in a library,
it means that the library is not optimised.
What is your opinion ?
Thanks.
Do you think that the 'e.hash == hash' is required ?
I think that the "if (k == e.key || k.equals(e.key))" is proper.
For further details,
the following codes are parts of 'HashMap'.
<code>
public V put(K key, V value) {
K k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table; e != null; e = e.next) {
if (e.hash == hash && eq(k, e.key)) { // #1
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
...
static int indexFor(int h, int length) {
return h & (length-1); // #2
}
static boolean eq(Object x, Object y) {
return x == y || x.equals(y); // #3
}
</code>
The linked list of the 'table' is heterogeneous with
hash value. That is, when hash value of 'E0' is 5
and one of 'E1' is 6, both 'E0' and 'E1' can exist in
the same bucket 'table' because of the above #2
( 00 & 10 = 00, 01 & 10 = 00).
But, it do not justify the code "if (e.hash == hash" of #1.
Because, ...
a) The functions of 'Map' puts/gets the value 'V' that is
mapped by the key 'K', not by the hash value.
b) The execution time of "k == e.key" by #3 is not large
than one of 'e.hash == hash'.
Therefore, I think,
The code "if (k == e.key || k.equals(e.key))" is proper
instead of #1. #1 is overabundance.
I think that if there is overabundance in a library,
it means that the library is not optimised.
What is your opinion ?
Thanks.