Tabnine Logo
HashMap.hash
Code IndexAdd Tabnine to your IDE (free)

How to use
hash
method
in
java.util.HashMap

Best Java code snippets using java.util.HashMap.hash (Showing top 20 results out of 1,413)

origin: stackoverflow.com

java.lang.Thread.State: RUNNABLE
 at java.util.HashMap.hash(HashMap.java:351)
 at java.util.HashMap.putForCreate(HashMap.java:512)
 at java.util.HashMap.putAllForCreate(HashMap.java:534)
 at java.util.HashMap.<init>(HashMap.java:320)
origin: org.apidesign.bck2brwsr/emul

/**
 * Returns the entry associated with the specified key in the
 * HashMap.  Returns null if the HashMap contains no mapping
 * for the key.
 */
final Entry<K,V> getEntry(Object key) {
  int hash = (key == null) ? 0 : hash(key.hashCode());
  for (Entry<K,V> e = table[indexFor(hash, table.length)];
     e != null;
     e = e.next) {
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k))))
      return e;
  }
  return null;
}
origin: jtulach/bck2brwsr

/**
 * Returns the entry associated with the specified key in the
 * HashMap.  Returns null if the HashMap contains no mapping
 * for the key.
 */
final Entry<K,V> getEntry(Object key) {
  int hash = (key == null) ? 0 : hash(key.hashCode());
  for (Entry<K,V> e = table[indexFor(hash, table.length)];
     e != null;
     e = e.next) {
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k))))
      return e;
  }
  return null;
}
origin: org.apidesign.bck2brwsr/emul

/**
 * 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(K key, V value) {
  int hash = (key == null) ? 0 : hash(key.hashCode());
  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<K,V> e = table[i]; e != null; e = e.next) {
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k)))) {
      e.value = value;
      return;
    }
  }
  createEntry(hash, key, value, i);
}
origin: stackoverflow.com

 Map<K,V> map = new HashMap<K,V>(){
public Entry<K,V> get(Object key) { // overloading get method in subclass
   if (key == null)
     return getForNullKey();
   int hash = hash(key.hashCode());
   for (Entry<K,V> e = table[indexFor(hash, table.length)];
     e != null;
     e = e.next) {
     Object k;
     if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
       return e;
   }
   return null;
 }


 private Entry<K,V> getForNullKey() { 
   for (Entry<K,V> e = table[0]; e != null; e = e.next) {
     if (e.key == null)
       return e;
   }
   return null;
 }};
 ...
 Map.Entry<K,V> entry1 = map.get(key);// invoking Entry<K,V> get(Object key)
origin: jtulach/bck2brwsr

/**
 * 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(K key, V value) {
  int hash = (key == null) ? 0 : hash(key.hashCode());
  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<K,V> e = table[i]; e != null; e = e.next) {
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k)))) {
      e.value = value;
      return;
    }
  }
  createEntry(hash, key, value, i);
}
origin: org.apidesign.bck2brwsr/emul

/**
 * Removes and returns the entry associated with the specified key
 * in the HashMap.  Returns null if the HashMap contains no mapping
 * for this key.
 */
final Entry<K,V> removeEntryForKey(Object key) {
  int hash = (key == null) ? 0 : hash(key.hashCode());
  int i = indexFor(hash, table.length);
  Entry<K,V> prev = table[i];
  Entry<K,V> e = prev;
  while (e != null) {
    Entry<K,V> next = e.next;
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k)))) {
      modCount++;
      size--;
      if (prev == e)
        table[i] = next;
      else
        prev.next = next;
      e.recordRemoval(this);
      return e;
    }
    prev = e;
    e = next;
  }
  return e;
}
origin: org.apidesign.bck2brwsr/emul

/**
 * 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, Object)
 */
public V get(Object key) {
  if (key == null)
    return getForNullKey();
  int hash = hash(key.hashCode());
  for (Entry<K,V> e = table[indexFor(hash, table.length)];
     e != null;
     e = e.next) {
    Object k;
    if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
      return e.value;
  }
  return null;
}
origin: jtulach/bck2brwsr

/**
 * Removes and returns the entry associated with the specified key
 * in the HashMap.  Returns null if the HashMap contains no mapping
 * for this key.
 */
final Entry<K,V> removeEntryForKey(Object key) {
  int hash = (key == null) ? 0 : hash(key.hashCode());
  int i = indexFor(hash, table.length);
  Entry<K,V> prev = table[i];
  Entry<K,V> e = prev;
  while (e != null) {
    Entry<K,V> next = e.next;
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k)))) {
      modCount++;
      size--;
      if (prev == e)
        table[i] = next;
      else
        prev.next = next;
      e.recordRemoval(this);
      return e;
    }
    prev = e;
    e = next;
  }
  return e;
}
origin: org.apidesign.bck2brwsr/emul

/**
 * Special version of remove for EntrySet.
 */
final Entry<K,V> removeMapping(Object o) {
  if (!(o instanceof Map.Entry))
    return null;
  Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
  Object key = entry.getKey();
  int hash = (key == null) ? 0 : hash(key.hashCode());
  int i = indexFor(hash, table.length);
  Entry<K,V> prev = table[i];
  Entry<K,V> e = prev;
  while (e != null) {
    Entry<K,V> 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;
}
origin: jtulach/bck2brwsr

/**
 * Special version of remove for EntrySet.
 */
final Entry<K,V> removeMapping(Object o) {
  if (!(o instanceof Map.Entry))
    return null;
  Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
  Object key = entry.getKey();
  int hash = (key == null) ? 0 : hash(key.hashCode());
  int i = indexFor(hash, table.length);
  Entry<K,V> prev = table[i];
  Entry<K,V> e = prev;
  while (e != null) {
    Entry<K,V> 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;
}
origin: org.apidesign.bck2brwsr/emul

public void remove() {
  if (lastReturned == null)
    throw new IllegalStateException();
  if (modCount != expectedModCount)
    throw new ConcurrentModificationException();
  WeakHashMap.this.remove(currentKey);
  expectedModCount = modCount;
  lastReturned = null;
  currentKey = null;
}
origin: jtulach/bck2brwsr

public void remove() {
  if (lastReturned == null)
    throw new IllegalStateException();
  if (modCount != expectedModCount)
    throw new ConcurrentModificationException();
  WeakHashMap.this.remove(currentKey);
  expectedModCount = modCount;
  lastReturned = null;
  currentKey = null;
}
origin: org.apidesign.bck2brwsr/emul

/** Special version of remove needed by Entry set */
boolean removeMapping(Object o) {
  if (!(o instanceof Map.Entry))
    return false;
  Entry<K,V>[] tab = getTable();
  Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
  Object k = maskNull(entry.getKey());
  int h = HashMap.hash(k.hashCode());
  int i = indexFor(h, tab.length);
  Entry<K,V> prev = tab[i];
  Entry<K,V> e = prev;
  while (e != null) {
    Entry<K,V> next = e.next;
    if (h == e.hash && e.equals(entry)) {
      modCount++;
      size--;
      if (prev == e)
        tab[i] = next;
      else
        prev.next = next;
      return true;
    }
    prev = e;
    e = next;
  }
  return false;
}
origin: jtulach/bck2brwsr

/** Special version of remove needed by Entry set */
boolean removeMapping(Object o) {
  if (!(o instanceof Map.Entry))
    return false;
  Entry<K,V>[] tab = getTable();
  Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
  Object k = maskNull(entry.getKey());
  int h = HashMap.hash(k.hashCode());
  int i = indexFor(h, tab.length);
  Entry<K,V> prev = tab[i];
  Entry<K,V> e = prev;
  while (e != null) {
    Entry<K,V> next = e.next;
    if (h == e.hash && e.equals(entry)) {
      modCount++;
      size--;
      if (prev == e)
        tab[i] = next;
      else
        prev.next = next;
      return true;
    }
    prev = e;
    e = next;
  }
  return false;
}
origin: org.apidesign.bck2brwsr/emul

/**
 * Returns the entry associated with the specified key in this map.
 * Returns null if the map contains no mapping for this key.
 */
Entry<K,V> getEntry(Object key) {
  Object k = maskNull(key);
  int h = HashMap.hash(k.hashCode());
  Entry<K,V>[] tab = getTable();
  int index = indexFor(h, tab.length);
  Entry<K,V> e = tab[index];
  while (e != null && !(e.hash == h && eq(k, e.get())))
    e = e.next;
  return e;
}
origin: org.apidesign.bck2brwsr/emul

/**
 * 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 V put(K key, V value) {
  if (key == null)
    return putForNullKey(value);
  int hash = hash(key.hashCode());
  int i = indexFor(hash, table.length);
  for (Entry<K,V> e = table[i]; e != null; e = e.next) {
    Object k;
    if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
      V oldValue = e.value;
      e.value = value;
      e.recordAccess(this);
      return oldValue;
    }
  }
  modCount++;
  addEntry(hash, key, value, i);
  return null;
}
origin: jtulach/bck2brwsr

/**
 * Returns the entry associated with the specified key in this map.
 * Returns null if the map contains no mapping for this key.
 */
Entry<K,V> getEntry(Object key) {
  Object k = maskNull(key);
  int h = HashMap.hash(k.hashCode());
  Entry<K,V>[] tab = getTable();
  int index = indexFor(h, tab.length);
  Entry<K,V> e = tab[index];
  while (e != null && !(e.hash == h && eq(k, e.get())))
    e = e.next;
  return e;
}
origin: org.apidesign.bck2brwsr/emul

/**
 * 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, Object)
 */
public V get(Object key) {
  Object k = maskNull(key);
  int h = HashMap.hash(k.hashCode());
  Entry<K,V>[] tab = getTable();
  int index = indexFor(h, tab.length);
  Entry<K,V> e = tab[index];
  while (e != null) {
    if (e.hash == h && eq(k, e.get()))
      return e.value;
    e = e.next;
  }
  return null;
}
origin: org.apidesign.bck2brwsr/emul

int h = HashMap.hash(k.hashCode());
Entry<K,V>[] tab = getTable();
int i = indexFor(h, tab.length);
java.utilHashMaphash

Javadoc

Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because HashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower bits. Note: Null keys always map to hash 0, thus index 0.

Popular methods of HashMap

  • <init>
    Constructs a new HashMap instance containing the mappings from the specified map.
  • put
    Maps the specified key to the specified value.
  • get
    Returns the value of the mapping with the specified key.
  • containsKey
    Returns whether this map contains the specified key.
  • keySet
    Returns a set of the keys contained in this map. The set is backed by this map so changes to one are
  • remove
  • entrySet
    Returns a set containing all of the mappings in this map. Each mapping is an instance of Map.Entry.
  • values
    Returns a collection of the values contained in this map. The collection is backed by this map so ch
  • size
    Returns the number of elements in this map.
  • clear
    Removes all mappings from this hash map, leaving it empty.
  • isEmpty
    Returns whether this map is empty.
  • putAll
    Copies all the mappings in the specified map to this map. These mappings will replace all mappings t
  • isEmpty,
  • putAll,
  • clone,
  • toString,
  • containsValue,
  • equals,
  • hashCode,
  • computeIfAbsent,
  • getOrDefault,
  • forEach

Popular in Java

  • Parsing JSON documents to java classes using gson
  • setRequestProperty (URLConnection)
  • getResourceAsStream (ClassLoader)
  • putExtra (Intent)
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • AtomicInteger (java.util.concurrent.atomic)
    An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati
  • Manifest (java.util.jar)
    The Manifest class is used to obtain attribute information for a JarFile and its entries.
  • DataSource (javax.sql)
    An interface for the creation of Connection objects which represent a connection to a database. This
  • Response (javax.ws.rs.core)
    Defines the contract between a returned instance and the runtime when an application needs to provid
  • FileUtils (org.apache.commons.io)
    General file manipulation utilities. Facilities are provided in the following areas: * writing to a
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now