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

x33g5p2x  于2022-01-17 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(116)

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

ConcurrentHashMap.hash介绍

[英]Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because ConcurrentHashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower or upper bits.
[中]将补充哈希函数应用于给定的哈希代码,以防止出现低质量的哈希函数。这是至关重要的,因为ConcurrentHashMap使用两个长度哈希表的幂,否则会遇到低位或高位不不同的哈希代码冲突。

代码示例

代码示例来源:origin: robovm/robovm

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: robovm/robovm

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if the specified key is null
 */
public boolean remove(Object key, Object value) {
  int hash = hash(key.hashCode());
  Segment<K,V> s;
  return value != null && (s = segmentForHash(hash)) != null &&
    s.remove(key, hash, value) != null;
}

代码示例来源:origin: robovm/robovm

/**
 * {@inheritDoc}
 *
 * @return the previous value associated with the specified key,
 *         or <tt>null</tt> if there was no mapping for the key
 * @throws NullPointerException if the specified key or value is null
 */
public V replace(K key, V value) {
  int hash = hash(key.hashCode());
  if (value == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s == null ? null : s.replace(key, hash, value);
}

代码示例来源:origin: robovm/robovm

/**
 * {@inheritDoc}
 *
 * @return the previous value associated with the specified key,
 *         or <tt>null</tt> if there was no mapping for the key
 * @throws NullPointerException if the specified key or value is null
 */
@SuppressWarnings("unchecked")
public V putIfAbsent(K key, V value) {
  Segment<K,V> s;
  if (value == null)
    throw new NullPointerException();
  int hash = hash(key.hashCode());
  int j = (hash >>> segmentShift) & segmentMask;
  if ((s = (Segment<K,V>)UNSAFE.getObject
     (segments, (j << SSHIFT) + SBASE)) == null)
    s = ensureSegment(j);
  return s.put(key, hash, value, true);
}

代码示例来源:origin: robovm/robovm

/**
 * Removes the key (and its corresponding value) from this map.
 * This method does nothing if the key is not in the map.
 *
 * @param  key the key that needs to be removed
 * @return the previous value associated with <tt>key</tt>, or
 *         <tt>null</tt> if there was no mapping for <tt>key</tt>
 * @throws NullPointerException if the specified key is null
 */
public V remove(Object key) {
  int hash = hash(key.hashCode());
  Segment<K,V> s = segmentForHash(hash);
  return s == null ? null : s.remove(key, hash, null);
}

代码示例来源:origin: robovm/robovm

/**
 * Maps the specified key to the specified value in this table.
 * Neither the key nor the value can be null.
 *
 * <p> The value can be retrieved by calling the <tt>get</tt> method
 * with a key that is equal to the original key.
 *
 * @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>
 * @throws NullPointerException if the specified key or value is null
 */
@SuppressWarnings("unchecked")
public V put(K key, V value) {
  Segment<K,V> s;
  if (value == null)
    throw new NullPointerException();
  int hash = hash(key.hashCode());
  int j = (hash >>> segmentShift) & segmentMask;
  if ((s = (Segment<K,V>)UNSAFE.getObject          // nonvolatile; recheck
     (segments, (j << SSHIFT) + SBASE)) == null) //  in ensureSegment
    s = ensureSegment(j);
  return s.put(key, hash, value, false);
}

代码示例来源:origin: robovm/robovm

/**
 * 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.equals(k)},
 * then this method returns {@code v}; otherwise it returns
 * {@code null}.  (There can be at most one such mapping.)
 *
 * @throws NullPointerException if the specified key is null
 */
public V get(Object key) {
  Segment<K,V> s; // manually integrate access methods to reduce overhead
  HashEntry<K,V>[] tab;
  int h = hash(key.hashCode());
  long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
  if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
    (tab = s.table) != null) {
    for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
         (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
       e != null; e = e.next) {
      K k;
      if ((k = e.key) == key || (e.hash == h && key.equals(k)))
        return e.value;
    }
  }
  return null;
}

代码示例来源:origin: robovm/robovm

/**
 * Tests if the specified object is a key in this table.
 *
 * @param  key   possible key
 * @return <tt>true</tt> if and only if the specified object
 *         is a key in this table, as determined by the
 *         <tt>equals</tt> method; <tt>false</tt> otherwise.
 * @throws NullPointerException if the specified key is null
 */
@SuppressWarnings("unchecked")
public boolean containsKey(Object key) {
  Segment<K,V> s; // same as get() except no need for volatile value read
  HashEntry<K,V>[] tab;
  int h = hash(key.hashCode());
  long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
  if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
    (tab = s.table) != null) {
    for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
         (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
       e != null; e = e.next) {
      K k;
      if ((k = e.key) == key || (e.hash == h && key.equals(k)))
        return true;
    }
  }
  return false;
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: org.codehaus.jsr166-mirror/jsr166

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: MobiVM/robovm

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: ibinti/bugvm

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: com.jtransc/jtransc-rt

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K, V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: com.bugvm/bugvm-rt

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if any of the arguments are null
 */
public boolean replace(K key, V oldValue, V newValue) {
  int hash = hash(key.hashCode());
  if (oldValue == null || newValue == null)
    throw new NullPointerException();
  Segment<K,V> s = segmentForHash(hash);
  return s != null && s.replace(key, hash, oldValue, newValue);
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if the specified key is null
 */
public boolean remove(Object key, Object value) {
  int hash = hash(key.hashCode());
  Segment<K,V> s;
  return value != null && (s = segmentForHash(hash)) != null &&
    s.remove(key, hash, value) != null;
}

代码示例来源:origin: com.bugvm/bugvm-rt

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if the specified key is null
 */
public boolean remove(Object key, Object value) {
  int hash = hash(key.hashCode());
  Segment<K,V> s;
  return value != null && (s = segmentForHash(hash)) != null &&
    s.remove(key, hash, value) != null;
}

代码示例来源:origin: ibinti/bugvm

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if the specified key is null
 */
public boolean remove(Object key, Object value) {
  int hash = hash(key.hashCode());
  Segment<K,V> s;
  return value != null && (s = segmentForHash(hash)) != null &&
    s.remove(key, hash, value) != null;
}

代码示例来源:origin: MobiVM/robovm

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if the specified key is null
 */
public boolean remove(Object key, Object value) {
  int hash = hash(key.hashCode());
  Segment<K,V> s;
  return value != null && (s = segmentForHash(hash)) != null &&
    s.remove(key, hash, value) != null;
}

代码示例来源:origin: com.gluonhq/robovm-rt

/**
 * {@inheritDoc}
 *
 * @throws NullPointerException if the specified key is null
 */
public boolean remove(Object key, Object value) {
  int hash = hash(key.hashCode());
  Segment<K,V> s;
  return value != null && (s = segmentForHash(hash)) != null &&
    s.remove(key, hash, value) != null;
}

代码示例来源:origin: MobiVM/robovm

/**
 * Removes the key (and its corresponding value) from this map.
 * This method does nothing if the key is not in the map.
 *
 * @param  key the key that needs to be removed
 * @return the previous value associated with <tt>key</tt>, or
 *         <tt>null</tt> if there was no mapping for <tt>key</tt>
 * @throws NullPointerException if the specified key is null
 */
public V remove(Object key) {
  int hash = hash(key.hashCode());
  Segment<K,V> s = segmentForHash(hash);
  return s == null ? null : s.remove(key, hash, null);
}

相关文章