==,hashcde, equals(一)

1、Hash  的属性, 

    1)bucket 和 list

2、java.lang.object 的 hashcode 和 equal 通过内存地址比较

3、为什么要重写hashcode 和 equals, 

     1) 实现hashset不可重复的特性(http://www.cnblogs.com/happyPawpaw/p/3744971.html)

      2)Hashmap 

4、Hashmap的键值如果是类

5、Hashmap的put 和 get 方法

    https://blog.csdn.net/lan12334321234/article/details/70048493   

    (https://blog.csdn.net/VIP_WangSai/article/details/77505517)

6、先判断hashcode, 再判断equals 

     为什么要这种判断顺序呢,因为要实现的是hash的特性,bucket由hashcode 决定;

      hashcode  true, equal 不一定 true

      equals true, hashcode 一定true

==,hashcde, equals

public class Student{
    private int id;
    private String name;
    
    public Student(int id, String name){
        this.id = id;
        this.name = name;
    }    
}

  

main

public static void main(String args[]) { 
        
        Student student01 = new Student(1,"lee");
        Student student02 = new Student(1,"lee");
        
        System.out.print("student01 == student02 :");
        if(student01 == student02){
            System.out.println("true.");
        }else{
            System.out.println("false.");
        }
        
        System.out.print("student01.hashCode() == student02.hashCode() :");
        if(student01.hashCode() == student02.hashCode()){
            System.out.println("true.");
        }else{
            System.out.println("false.");
        }
        
        System.out.print("student01.equals(student02) :");
        if(student01.equals(student02)){
            System.out.println("true.");
        }else{
            System.out.println("false.");
        }
        
         Set<Object> set = new HashSet<Object>();
         set.add(student01);
         set.add(student02);
         System.out.println("HashSet 长度:" + set.size());
    }

输出

默认,继承Object
student01 == student02 :false.
student01.hashCode() == student02.hashCode() :false.
student01.equals(student02) :false.
HashSet 长度:2  

修改student 的 hashcode类如下

public class StudentModifyHashCode{
	
	private int id;
	private String name;
	
	public StudentModifyHashCode(int id, String name){
		this.id = id;
		this.name = name;
	}
	
	@Override
	public int hashCode(){
		final int prime = 31;
		int result = 1;
		result = prime*result+this.id;
		return result;
	}
}

  

输出,可以看到这次hashcode 一样的

修改hashcode
student01 == student02 :false.
student01.hashCode() == student02.hashCode() :true.
student01.equals(student02) :false.
HashSet 长度:2

  

同时修改student的hashcode 和 equals 方法如下

public class StudentModifyEqualAndHash{
    
    private int id;
    private String name;
    
    public StudentModifyEqualAndHash(int id, String name){
        this.id = id;
        this.name = name;
    }
    
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode(){
        final int prime = 31;
        int result = 1;
        result = prime*result+this.id;
        return result;
    }
    
    @Override
    public boolean equals(Object obj){
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(getClass() != obj.getClass())
            return false;
        StudentModifyEqualAndHash other = (StudentModifyEqualAndHash)obj;        
        if(this.id != other.getId() ){         
            return false;
        } 
        for(int i=0;i<this.name.length();i++){    
                if(this.name.charAt(i) != (((StudentModifyEqualAndHash)obj).getName().charAt(i))){    
                      return false;
                }        
        }
        return true;
    }        
}

  

输出,可以看到hashcode 和 equals 都一样

修改hashcode和equals
student01 == student02 :false.
student01.hashCode() == student02.hashCode() :true.
student01.equals(student02) :true.
HashSet 长度:1

 https://blog.csdn.net/lixiaoxiong55/article/details/93376852

 hashCode作用: https://blog.csdn.net/qq_38977097/article/details/80834525

 

原文地址:https://www.cnblogs.com/Jomini/p/9638254.html