• java 中的equals hashCode


    简介

    hashCode 与 equals 应该有等同性,如果A equals B 那么 A 和 B 的hashCode 应该一样
    还有toString方法可以在后期调试的过程中起到很好的作用

    code

    EXAMPLE

    package com;
    
    public class EqualsTest {
    	public static void main(String[] args){
    		Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
    		Employee alice2 = alice1;
    		Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
    		Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
    		System.out.println("alice1 == alice2: " + (alice1 == alice2));
    		System.out.println("alice1 == alice3: " + (alice1 == alice3));
    		System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
    		System.out.println("alice1.equals(bob): " + alice1.equals(bob));
    		System.out.println("bob.toString(): " + bob);
    		Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    		Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    		boss.setBonus(5000);
    		System.out.println("boss.toString(): " + boss);
    		System.out.println("carl.equals(boss): " + carl.equals(boss));
    		System.out.println("alice1.hashCode(): " + alice1.hashCode());
    		System.out.println("alice3.hashCode(): " + alice3.hashCode());
    		System.out.println("bob.hashCode(): " + bob.hashCode());
    		System.out.println("carl.hashCode(): " + carl.hashCode());
    	}
    }
    
    
    package com;
    
    public class Manager extends Employee{
    	private double bonus;
    	public Manager(String name, double salary, int year, int month, int day) {
    		super(name, salary, year, month, day);
    		bonus = 0;
    	}
    	
    	public double getSalary() {
    		double baseSalary = super.getSalary();
    		return baseSalary + bonus;
    	}
    	
    	public void setBonus(double bonus) {
    		this.bonus = bonus;
    	}
    	
    	public boolean equals(Object otherObject){
    		if (!super.equals(otherObject)) return false;
    		Manager other = (Manager) otherObject;
    		return bonus == other.bonus;
    	}
    	
    	public int hashCode(){
    		return super.hashCode() + 17 * new Double(bonus).hashCode();
    	}
    	
    	public String toString() {
    		return super.toString() + "[bonus=" + bonus + "]";
    	}
    }
    
    
    package com;
    
    import java.time.*;
    import java.util.Objects;
    public class Employee{
    	private String name;
    	private double salary;
    	private LocalDate hireDay;
    	
    	public Employee(String name, double salary, int year, int month, int day){
    		this.name = name;
    		this.salary = salary;
    		hireDay = LocalDate.of(year, month, day);
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public double getSalary() {
    		return salary;
    	}
    	
    	public LocalDate getHireDay() {
    		return hireDay;
    	}
    	
    	public String getDescription() {
    		return String.format("an emplyee with a salary of $%.2f", salary);
    	}
    	
    	public void raiseSalary(double byPercent) {
    		double raise = salary * byPercent / 100;
    		salary += raise;
    	}
    	
    	public boolean equals(Object otherObject) {
    		if (this == otherObject) return true;
    		if (otherObject == null) return false;
    		if (getClass() != otherObject.getClass()) return false;
    		
    		Employee other = (Employee) otherObject;
    		return Objects.equals(name, other.name) && salary == other.salary
    				&& Objects.equals(hireDay,  other.hireDay);
    	}
    	
    	public int hashCode(){
    		return Objects.hash(name, salary, hireDay);
    	}
    	
    	public String toString() {
    		return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
    	}
    }
    

    Answer

    alice1 == alice2: true
    alice1 == alice3: false
    alice1.equals(alice3): true
    alice1.equals(bob): false
    bob.toString(): com.Employee[name=Bob Brandson,salary=50000.0,hireDay=1989-10-01]
    boss.toString(): com.Manager[name=Carl Cracker,salary=80000.0,hireDay=1987-12-15][bonus=5000.0]
    carl.equals(boss): false
    alice1.hashCode(): -808853550
    alice3.hashCode(): -808853550
    bob.hashCode(): -624019882
    carl.hashCode(): -341762419
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    C# winform开发:Graphics、pictureBox同时画多个矩形
    C# “配置系统未能初始化” 异常解决
    Google Maps API V3 之 路线服务
    Google Maps API V3 之 图层
    Google Maps API V3 之绘图库 信息窗口
    Google 地图 API V3 之 叠加层
    驱动开发之libusb函数
    libusb的使用教程和例子
    libusb检测U盘插入
    使用libusb检测USB设备插拔状态
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13396904.html
Copyright © 2020-2023  润新知