zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    【小白学PyTorch】1 搭建一个超简单的网络
    【小白学PyTorch】2 浅谈训练集和测试集
    【小白学AI】GBDT梯度提升详解
    【小白学AI】XGBoost推导详解与牛顿法
    【小白写论文】技术性论文结构剖析
    小白学PyTorch 动态图与静态图的浅显理解
    【小白学推荐1】 协同过滤 零基础到入门
    【小白学AI】随机森林 全解 (从bagging到variance)
    OpenCV开发笔记(七十二):红胖子8分钟带你使用opencv+dnn+tensorFlow识别物体
    【python刷题】二叉搜索树-相关题目
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13396904.html
Copyright © 2011-2022 走看看