/** * Returns the entry associated with the specified key in the IntHashMap. Returns null if the * IntHashMap contains no mapping for the key. */ Entry getEntry(Object key) { int hash = (key == null) ? 0 : hash(hashingStrategy.hashCode(key)); for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && hashingStrategy.equals(k, key)))) return e; } return null; }
/** * This method is used instead of put by constructors and pseudoconstructors (clone, readObject). * It does not resize the table, check for comodification, etc. It calls createEntry rather than * addEntry. */ private void putForCreate(Object key, int value) { int hash = (key == null) ? 0 : hash(hashingStrategy.hashCode(key)); int i = indexFor(hash, table.length); /** * Look for preexisting entry for key. This will never happen for clone or deserialize. It will * only happen for construction if the input Map is a sorted map whose ordering is inconsistent * w/ equals. */ for (Entry e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && hashingStrategy.equals(k, key)))) { e.value = value; return; } } createEntry(hash, key, value, i); }
/** * Removes and returns the entry associated with the specified key in the IntHashMap. Returns null * if the IntHashMap contains no mapping for this key. */ Entry removeEntryForKey(Object key) { int hash = (key == null) ? 0 : hash(hashingStrategy.hashCode(key)); int i = indexFor(hash, table.length); Entry prev = table[i]; Entry e = prev; while (e != null) { Entry next = e.next; Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && hashingStrategy.equals(k, key)))) { modCount++; size--; if (prev == e) table[i] = next; else prev.next = next; e.recordRemoval(this); return e; } prev = e; e = next; } return e; }
/** * Returns the value to which the specified key is mapped, or {@code null} if this map contains no * mapping for the key. * * <p> * More formally, if this map contains a mapping from a key {@code k} to a value {@code v} such * that {@code (key==null ? k==null : * key.equals(k))}, then this method returns {@code v}; otherwise it returns {@code null}. (There * can be at most one such mapping.) * * <p> * A return value of {@code null} does not <i>necessarily</i> indicate that the map contains no * mapping for the key; it's also possible that the map explicitly maps the key to {@code null}. * The {@link #containsKey containsKey} operation may be used to distinguish these two cases. * * @see #put(Object, int) */ public int get(Object key) { if (key == null) return getForNullKey(); int hash = hash(hashingStrategy.hashCode(key)); for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || hashingStrategy.equals(k, key))) return e.value; } return 0; }
/** * Special version of remove for EntrySet. */ Entry removeMapping(Object o) { if (!(o instanceof Entry)) return null; Entry entry = (Entry) o; Object key = entry.getKey(); int hash = (key == null) ? 0 : hash(hashingStrategy.hashCode(key)); int i = indexFor(hash, table.length); Entry prev = table[i]; Entry e = prev; while (e != null) { Entry next = e.next; if (e.hash == hash && e.equals(entry)) { modCount++; size--; if (prev == e) table[i] = next; else prev.next = next; e.recordRemoval(this); return e; } prev = e; e = next; } return e; }
/** * Associates the specified value with the specified key in this map. If the map previously * contained a mapping for the key, the old value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if there was no * mapping for <tt>key</tt>. (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with <tt>key</tt>.) */ public int put(Object key, int value) { if (key == null) return putForNullKey(value); int hash = hash(hashingStrategy.hashCode(key)); int i = indexFor(hash, table.length); for (Entry e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || hashingStrategy.equals(k, key))) { int oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return 0; }