zoukankan      html  css  js  c++  java
  • HashCode和hashMap hashTable

    先看看Java中的HashCode 

    在Java中,哈希码代表对象的特征。

      例如对象 String str1 = “aa”, str1.hashCode= 3104
      String str2 = “bb”, str2.hashCode= 3106
      String str3 = “aa”, str3.hashCode= 3104
      根据HashCode由此可得出str1!=str2,str1==str3
      哈希码产生的依据:哈希码并不是完全唯一的,它是一种算法,让同一个类的对象按照自己不同的特征尽量的有不同的哈希码,但不表示不同的对象哈希码完全不同。也有相同的情况,看程序员如何写哈希码的算法。
      下面给出几个常用的哈希码的算法。
      1:Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样。
      2:String类的hashCode.根据String类包含的字符串的内容,根据一种特殊算法返回哈希码,只要字符串内容相同,返回的哈希码也相同。

      3:Integer类,返回的哈希码就是Integer对象里所包含的那个整数的数值,例如Integer i1=new Integer(100),i1.hashCode的值就是100 。由此可见,2个一样大小的Integer对象,返回的哈希码也一样。更多资料请参考这里

    类 HashMap<K,V>

    java.lang.Object
      java.util.AbstractMap<K,V>
          java.util.HashMap<K,V>
    
    类型参数:
    K - 此映射所维护的键的类型
    V - 所映射值的类型
    基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。

    HashCode和HashMap之间的关系

    先如下代码:

    [java] view plaincopy
     
    1. import java.util.HashMap;  
    2. public class Test {  
    3.   
    4.     //重写Equals不重写HashCode  
    5.     static class Key {  
    6.         private Integer id;  
    7.         private String value;  
    8.           
    9.         public Key(Integer id, String value) {  
    10.             super();  
    11.             this.id = id;  
    12.             this.value = value;  
    13.         }  
    14.         @Override  
    15.         public boolean equals(Object o) {  
    16.             if(o == null || !(o instanceof Key)) {  
    17.                 return false;  
    18.             }else {  
    19.                 return this.id.equals(((Key)o).id);  
    20.             }  
    21.         }  
    22.     }  
    23.     //重写Equals也重写HashCode  
    24.         static class Key_ {  
    25.             private Integer id;  
    26.             private String value;  
    27.               
    28.             public Key_(Integer id, String value) {  
    29.                 super();  
    30.                 this.id = id;  
    31.                 this.value = value;  
    32.             }  
    33.             @Override  
    34.             public boolean equals(Object o) {  
    35.                 if(o == null || !(o instanceof Key_)) {  
    36.                     return false;  
    37.                 }else {  
    38.                     return this.id.equals(((Key_)o).id);  
    39.                 }  
    40.             }  
    41.             @Override  
    42.             public int hashCode() {  
    43.                  return id.hashCode();  
    44.             }  
    45.                
    46.         }  
    47.     public static void main(String[] args) {  
    48.         //test hashcode  
    49.         HashMap<Object, String> values = new HashMap<Object, String>(5);  
    50.         Test.Key key1 =   new Test.Key(1, "one");  
    51.         Test.Key key2 =   new Test.Key(1, "one");  
    52.         System.out.println(key1.equals(key2));  
    53.         values.put(key1, "value 1");  
    54.         System.out.println(values.get(key2));  
    55.           
    56.         Test.Key_ key_1 =   new Test.Key_(1, "one");  
    57.         Test.Key_ key_2 =   new Test.Key_(1, "one");  
    58.         System.out.println(key_1.equals(key_2));  
    59.         System.out.println(key_1 == key_2);  
    60.         values.put(key_1, "value 1");  
    61.         System.out.println(values.get(key_2));  
    62.     }  
    63. }  

    输出如下:由上述例子可见: 只重写了equasl方法的Key类 在用做Hash中的键值的时候 两个equasl为true的对象不能获取相应 的Value的 而重写了hashCode方法和equals方法的key_类 两个相等的对象 可以获取同一个Value的,这样更符合生活中 的逻辑 HashMap对象是根据Key的hashCode来获取对应的Vlaue 因而两个HashCode相同的对象可以获取同一个Value

    原文:http://blog.csdn.net/diqye2011/article/details/7641406

  • 相关阅读:
    9、Spring Boot 2.x 集成 Thymeleaf
    【专题】Spring Boot 2.x 面试题
    8、Spring Boot 2.x 服务器部署
    7、Spring Boot 2.x 集成 Redis
    6、Spring Boot 2.x 集成 MyBatis
    5、Spring Boot 2.x 启动原理解析
    4、Spring Boot 2.x 自动配置原理
    3、Spring Boot 2.x 核心技术
    2、Spring Boot 2.x 快速入门
    centOS下安装JDK1.8.60,glassfish4.1.1以及MySQL
  • 原文地址:https://www.cnblogs.com/veins/p/3986813.html
Copyright © 2011-2022 走看看