Answers for "java override equals and hashcode"

1

java override equals and hashcode

// EXAMPLE CLASS
public class User {
    private String name;
    private int age;
    private String passport;
	
	@Override public boolean equals(Object o) {
        if (o == this)  {
          return true;
        }
        if (!(o instanceof User)) {
            return false;
        }

        User user = (User) o;

        return user.name.equals(name) && 
				user.age == age && 
          		user.passport.equals(passport);
    }
  
    @Override public int hashCode() {
        int result = 17;
        result = 31 * result + name.hashCode();
        result = 31 * result + age;
        result = 31 * result + passport.hashCode();
        return result;
    }
Posted by: Guest on April-15-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language