Java 重写equals方法为什么还必须重写hashCode方法

参考链接:http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html 

先查看Java String 的源码

private final char value[];

private int hash; // Default to 0

 equals方法

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

hashCode方法 

    /**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

计算字符串的hashCode是根据对应字符的ASCII来计算的。

例如字符串:“123”

数字ASCII 值
149
250
351

计算公式:49*31^2+50*31+51=48690

为什么需要重写equals方法的同时也重写hashCode方法,因为只重写了equals方法但不重写hashCode两个对象相同,但它们的hashCode值不一样,这样将它们放在set集合里,他们是不同的对象,它们的hashCode值不一样。

 

其次,为什么重写equals方法必须重写hashCode方法,因为这两个方法是有关联的。就像上面的例子都用到了value[]数组。

Object中的equals是比较的值是否相同,用“==”来比较

public boolean equals(Object obj) {
    return (this == obj);
}

测试代码:

package com.example.demo;

public class Person {
	
	private Integer id;
	private String username;
	private Integer age;
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Person(Integer id, String username, Integer age) {
		this.id = id;
		this.username = username;
		this.age = age;
	}
	
	public int hashCode() {
		final int prime = 31;
		int result =1;
		result = prime * 31 + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		
		if(obj == null) {
			return false;
		}
		
		if(obj instanceof Person) {
			Person person = (Person) obj;
			if(id == person.getId() && username.equals(person.getUsername()) && age == person.getAge()) {
				return true;
			}
			return false;
		}
		
		return false;
		
		
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", username=" + username + ", age=" + age + "]";
	}

测试运行:

        Person person1 = new Person(1, "Jerry", 28);
		Person person2 = new Person(1, "Jerry", 27);
		Set<Person> persons = new HashSet<Person>();
		persons.add(person1);
		persons.add(person2);
		
		System.out.println("person1 equals person2: "+person1.equals(person2)); // false
		System.out.println("persons size:"+persons.size()); // 2
		System.out.println("person1 hashCode: "+person1.hashCode()); // 962
		System.out.println("person2 hashCode: "+person2.hashCode()); // 962
		System.out.println("person1 toString:"+person1.toString()); // person1 toString:Person [id=1, username=Jerry, age=28]
		System.out.println("person2 toString:"+person2.toString()); // person2 toString:Person [id=1, username=Jerry, age=27]
		System.out.println(persons); // [Person [id=1, username=Jerry, age=28], Person [id=1, username=Jerry, age=27]]
		
		Integer a = 1;
		
		System.out.println("1 hashCode: "+ a.hashCode()); // 1 hashCode: 1
		
		String str1 = new String("123");
        String str2 = new String("123");
        System.out.println(str1 == str2);  //false
        System.out.println(str1.equals(str2));  //true

 

转载于:https://my.oschina.net/u/3781047/blog/1621330

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值