Rewrite equals () and hashcode ()

Rewrite equals ()

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

 

 @Override
    public int hashCode() {

// The following comments rewrite process can be organized according to the class attribute their own code. Uncommented source code as part of // final int prime = 31; // int result = 1; // result = prime * result + age.hashCode(); // result = prime * result + ((name == null) ? 0 : name.hashCode()); // return result; return Objects.hash(name, age);

 

    public static int hash(Object... values) {
        return Arrays.hashCode(values);
    }

  

public static int hashCode(Object a[]) {
        if (a == null)
            return 0;

        int result = 1;

        for (Object element : a)
            result = 31 * result + (element == null ? 0 : element.hashCode());

        return result;
    }

  

 

Guess you like

Origin www.cnblogs.com/Andrew520/p/11031530.html