Java学习笔记15:java中的hashcode哈希码、hash 算法

推荐一篇好文章:深入理解 hashcode 和 hash 算法https://blog.csdn.net/qq_38182963/article/details/78940047

为什么使用 hashcode ?
hashCode 存在的第一重要的原因就是在 HashMap(HashSet 其实就是HashMap) 中使用(其实Object 类的 hashCode 方法注释已经说明)
HashMap 之所以速度快,因为使用散列表。根据 key 的 hashcode值生成数组下标(通过内存地址直接查找,没有任何判断),时间复杂度完美情况下可以达到 n1(和数组相同,但是比数组用着爽多了,但是需要多出很多内存,相当于以空间换时间)。

在Java中,哈希码代表了对象的一种特征,例如我们判断某两个字符串是否==,如果其哈希码相等,则这两个字符串是相等的。

其次,哈希码是一种数据结构的算法。

常见的哈希码的算法有:

  1. Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样。

  2. String类的hashCode.根据String类包含的字符串的内容,根据一种特殊算法返回哈希码,只要字符串内容相同,返回的哈希码也相同。

  3. Integer类,返回的哈希码就是Integer对象里所包含的那个整数的数值,例如:

    Integer i1=new Integer(100);

i1.hashCode的值就是100 。由此可见,两个一样大小的Integer对象,返回的哈希码也一样。

4.布尔类型的哈希码: https://blog.csdn.net/github_38838414/article/details/80502964

public static void main(String []args)
{
    Boolean b2=true;
    Boolean b3=false;
    System.out.println("ture的hash值:"+b2.hashCode());
    System.out.println("false的hash值:"+b3.hashCode());
}

输出结果:

ture的hash值:1231 
false的hash值:1237

为什么是1231和1237,而不是我们经常用的1和0?

不管使用什么哈希算法,其哈希函数都要极可能避免冲突。简单地说,不同的值要落在不同的存贮单元。认真观察,很容易发现1231和1237都是素数。使用素数的好处在于,对于不同数目的存贮单元m,1231和1237基本上都与其互质,除非m等于或数倍于1231和1237。这种情况下,true和false也能落到不同的单元去。
其实这也就是一般hash算法取值的默认规则,取素数。

实例:

package create;

public class Demo1_hashCode {
	
	public static void main(String[] args){
		Object obj1=new Object();
		int hashCode=obj1.hashCode();
		System.out.println(hashCode);
	String s1="张三";
	String s2="张三";
	String s3="李四";
	System.out.println(s1.hashCode());
	System.out.println(s2.hashCode());
	System.out.println(s3.hashCode());
	Integer i1=new Integer(100);
	System.out.println(i1.hashCode());
	}

}

输出结果为:

31168322
774889
774889
842061
100

以下为转载代码:

package new_start1;
public class Test1 {
    class Person
    {
        public String name;
        public Person(String n)
        {
            this.name=n;
        }
        public Person(){}
    }
    public static void change(Person a)//改变对象a的name值
    {
        a.name="haha";
    }
    public static void main(String[] args) {    
        Test1 t=new Test1();
        Person p=t.new Person("zhangsan"); //实例一个对象p
        Person a=t.new Person();//又实例一个对象a
        System.out.println("未赋值前,两者的哈希码是不相同的:");
        System.out.println("a.hashCode="+a.hashCode()+"  "+"p.hashCode="+p.hashCode());
        System.out.println("a.toString()="+a.toString());
        System.out.println("p.toString()="+p.toString());
        /*
            未赋值前,两者的哈希码是不相同的:
            a.hashCode=366712642  p.hashCode=1829164700
            a.toString()=new_start1.Test1$Person@15db9742
            p.toString()=new_start1.Test1$Person@6d06d69c
         */
        a=p; 
        System.out.println("赋值后,两者的哈希码相同:");
        System.out.println("a.hashCode="+a.hashCode()+"  "+"p.hashCode="+p.hashCode());
        System.out.println("a.toString()="+a.toString());
        System.out.println("p.toString()="+p.toString());
        /*
             赋值后,两者的哈希码相同:
            a.hashCode=1829164700  p.hashCode=1829164700
            a.toString()=new_start1.Test1$Person@6d06d69c
            p.toString()=new_start1.Test1$Person@6d06d69c
         */
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30242987/article/details/85615718
今日推荐