• Java HashMap HashCode


    1.HashMap

      《1》遍历:HashMap无序遍历

        for(Map.Entry<String, String>m: map.entrySet()//键值对,允许键为空String
            {
                System.out.println(m.getKey()+" : "+m.getValue());
            }

     HashMap 源码:

    1. transient Entry[] table;  
    2.    
    3. static class Entry<K,V> implements Map.Entry<K,V> {  
    4.     final K key;  
    5.     V value;  
    6.     Entry<K,V> next;  
    7.     final int hash;  
    8.     ……  

    添加元素

      1. public V put(K key, V value) {  
      2.    // HashMap允许存放null键和null值。  
      3.    // 当key为null时,调用putForNullKey方法,将value放置在数组第一个位置。  
      4.    if (key == null)  
      5.        return putForNullKey(value);  
      6.    // 根据key的keyCode重新计算hash值。  
      7.    int hash = hash(key.hashCode());  
      8.    // 搜索指定hash值在对应table中的索引。  
      9.    int i = indexFor(hash, table.length);  
      10.    // 如果 i 索引处的 Entry 不为 null,通过循环不断遍历 e 元素的下一个元素。  
      11.    for (Entry<K,V> e = table[i]; e != null; e = e.next) {  
      12.        Object k;  
      13.       // 如果发现 i 索引处的链表的某个Entry的hash和新Entry的hash相等且两者的key相同,则新Entry覆盖旧Entry,返回。  
      14.        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
      15.            V oldValue = e.value;  
      16.            e.value = value;  
      17.            e.recordAccess(this);  
      18.            return oldValue;  
      19.        }  
      20.    }  
      21.    // 如果i索引处的Entry为null,表明此处还没有Entry。  
      22.    modCount++;  
      23.    // 将key、value添加到i索引处。  
      24.    addEntry(hash, key, value, i);  
      25.    return null; 

    读取元素

       

    1. public V get(Object key) {  
    2.     if (key == null)  
    3.         return getForNullKey();  
    4.     int hash = hash(key.hashCode());  
    5.     for (Entry<K,V> e = table[indexFor(hash, table.length)];  
    6.         e != null;  
    7.         e = e.next) {  
    8.         Object k;  
    9.         if (e.hash == hash && ((k = e.key) == key || key.equals(k)))  
    10.             return e.value;  
    11.     }  
    12.     return null;  
    13. }

    String equals

       

    1.  /** 
    2.     * Compares this string to the specified object.  The result is {@code 
    3.     * true} if and only if the argument is not {@code null} and is a {@code 
    4.     * String} object that represents the same sequence of characters as this 
    5.     * object. 
    6.     * 
    7.     * @param  anObject 
    8.     *         The object to compare this {@code String} against 
    9.     * 
    10.     * @return  {@code true} if the given object represents a {@code String} 
    11.     *          equivalent to this string, {@code false} otherwise 
    12.     * 
    13.     * @see  #compareTo(String) 
    14.     * @see  #equalsIgnoreCase(String) 
    15.     */  
    16.    public boolean equals(Object anObject) {  
    17. if (this == anObject) {  
    18.     return true;  
    19. }  
    20. if (anObject instanceof String) {  
    21.     String anotherString = (String)anObject;  
    22.     int n = count;  
    23.     if (n == anotherString.count) {  
    24.     char v1[] = value;  
    25.     char v2[] = anotherString.value;  
    26.     int i = offset;  
    27.     int j = anotherString.offset;  
    28.     while (n-- != 0) {  
    29.         if (v1[i++] != v2[j++])  
    30.         return false;  
    31.     }  
    32.     return true;  
    33.     }  
    34. }  
    35. return false;  
    36.    } 

    String hashcode

    1.   /** 
    2.     * Returns a hash code for this string. The hash code for a 
    3.     * <code>String</code> object is computed as 
    4.     * <blockquote><pre> 
    5.     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 
    6.     * </pre></blockquote> 
    7.     * using <code>int</code> arithmetic, where <code>s[i]</code> is the 
    8.     * <i>i</i>th character of the string, <code>n</code> is the length of 
    9.     * the string, and <code>^</code> indicates exponentiation. 
    10.     * (The hash value of the empty string is zero.) 
    11.     * 
    12.     * @return  a hash code value for this object. 
    13.     */  
    14.    public int hashCode() {  
    15. int h = hash;  
    16.        int len = count;  
    17. if (h == 0 && len > 0) {  
    18.     int off = offset;  
    19.     char val[] = value;  
    20.   
    21.            for (int i = 0; i < len; i++) {  
    22.                h = 31*h + val[off++];  
    23.            }  
    24.            hash = h;  
    25.        }  
    26.        return h;  
    27.    } 

    hash函数

    1. /** 
    2.     * Applies a supplemental hash function to a given hashCode, which 
    3.     * defends against poor quality hash functions.  This is critical 
    4.     * because HashMap uses power-of-two length hash tables, that 
    5.     * otherwise encounter collisions for hashCodes that do not differ 
    6.     * in lower bits. Note: Null keys always map to hash 0, thus index 0. 
    7.     */  
    8. tatic int hash(int h) {  
    9.        // This function ensures that hashCodes that differ only by  
    10.        // constant multiples at each bit position have a bounded  
    11.        // number of collisions (approximately 8 at default load factor).  
    12.        h ^= (h >>> 20) ^ (h >>> 12);  
    13.        return h ^ (h >>> 7) ^ (h >>> 4);  
    14.    }  
    15.   
    16.    /** 
    17.     * Returns index for hash code h. 
    18.     */  
    19.  static int indexFor(int h, int length) {  
    20.        return h & (length-1);  
  • 相关阅读:
    zookeeper集群搭建2.7
    hadoop集群环境搭建
    Kettle(6.0) 参数方式连接数据库
    kettle数据同步的5中方案
    kettle 合并记录步骤中的 关键字段和 比较字段的说明
    KETTLE常见问题和优化
    Hbase与Oracle的比较
    EHCache
    hdu2014 青年歌手大奖赛_评委会打分【C++】
    hdu2013 蟠桃记【C++】
  • 原文地址:https://www.cnblogs.com/HackHer/p/5079492.html
Copyright © 2020-2023  润新知