java.util.HashMap.hash()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(214)

本文整理了Java中java.util.HashMap.hash()方法的一些代码示例,展示了HashMap.hash()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashMap.hash()方法的具体详情如下:
包路径:java.util.HashMap
类名称:HashMap
方法名:hash

HashMap.hash介绍

[英]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.
[中]将补充哈希函数应用于给定的哈希代码,以防止出现低质量的哈希函数。这是至关重要的,因为HashMap使用两个长度哈希表的幂,否则会遇到低位不不同的哈希代码冲突。注意:空键总是映射到散列0,因此索引为0。

代码示例

代码示例来源: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);

相关文章