public V put(K key, V value) { V oldValue = null; int index = Math.abs(key.hashCode()) % SIZE; if (buckets[index] == null) buckets[index] = new ArrayList<MapEntry<K, V>>(); ArrayList<MapEntry<K, V>> bucket = buckets[index]; MapEntry<K, V> pair = new MapEntry<K, V>(key, value); boolean found = false; ListIterator<MapEntry<K, V>> it = bucket.listIterator(); // int probes=0; while (it.hasNext()) { // probes++; MapEntry<K, V> iPair = it.next(); // if(!iPair.getKey().equals(key)) System.out.println("HashCode collision on put, have // key:"+key+" hascode:"+key.hashCode()+" found key:"+iPair.getKey()+" hashcode:"+ // iPair.getKey().hashCode()); if (iPair.getKey().equals(key)) { oldValue = iPair.getValue(); it.set(pair); // Replace old with new found = true; break; } } // System.out.println("put() probes for key "+ key+" :"+probes); if (!found) buckets[index].add(pair); return oldValue; }
final long hash(K key) { if (isCharSequence) { h = Maths.hash((CharSequence) key); } else if (isLongHashable) { h = ((LongHashable) key).longHashCode(); } else { h = (long) key.hashCode() << 31; } h += (h >>> 42) - (h >>> 21); h += (h >>> 14) - (h >>> 7); return h >>> segmentShift; }
public V put(K key, V value) { V oldValue = null; int index = Math.abs(key.hashCode()) % SIZE; if (buckets[index] == null) buckets[index] = new LinkedList<>(); LinkedList<MapEntry<K, V>> bucket = buckets[index]; MapEntry<K, V> pair = new MapEntry<>(key, value); boolean found = false; ListIterator<MapEntry<K, V>> it = bucket.listIterator(); while (it.hasNext()) { MapEntry<K, V> iPair = it.next(); if (iPair.getKey().equals(key)) { oldValue = iPair.getValue(); it.set(pair); // Replace old with new found = true; break; } } if (!found) buckets[index].add(pair); return oldValue; }
public int hashCode() { int code = 17; code += key.hashCode(); code *= 37; return code; }
public int hashCode() { return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); }