How to override equals method in Java
//Written by K@stackoverflowpublic class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here ArrayList<Person> people = new ArrayList<Person>(); people.add(new Person("Subash Adhikari", 28)); people.add(new Person("K", 28)); people.add(new Person("StackOverflow", 4)); people.add(new Person("Subash Adhikari", 28)); for (int i = 0; i < people.size() - 1; i++) { for (int y = i + 1; y <= people.size() - 1; y++) { boolean check = people.get(i).equals(people.get(y)); System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName()); System.out.println(check); } } }}//written by K@stackoverflowpublic class Person { private String name; private int age; public Person(String name, int age){ this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj.getClass() != this.getClass()) { return false; } final Person other = (Person) obj; if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { return false; } if (this.age != other.age) { return false; } return true; } @Override public int hashCode() { int hash = 3; hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0); hash = 53 * hash + this.age; return hash; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
Output:
run:
-- Subash Adhikari - VS - K false
-- Subash Adhikari - VS - StackOverflow false
-- Subash Adhikari - VS - Subash Adhikari true
-- K - VS - StackOverflow false
-- K - VS - Subash Adhikari false
-- StackOverflow - VS - Subash Adhikari false
-- BUILD SUCCESSFUL (total time: 0 seconds)
Introducing a new method signature that changes the parameter types is called overloading:
public boolean equals(People other){
Here People
is different than Object
.
When a method signature remains the identical to that of its superclass, it is called overriding and the @Override
annotation helps distinguish the two at compile-time:
@Overridepublic boolean equals(Object other){
Without seeing the actual declaration of age
, it is difficult to say why the error appears.
I'm not sure of the details as you haven't posted the whole code, but:
- remember to override
hashCode()
as well - the
equals
method should haveObject
, notPeople
as its argument type. At the moment you are overloading, not overriding, the equals method, which probably isn't what you want, especially given that you check its type later. - you can use
instanceof
to check it is a People object e.g.if (!(other instanceof People)) { result = false;}
equals
is used for all objects, but not primitives. I think you mean age is anint
(primitive), in which case just use==
. Note that an Integer (with a capital 'I') is an Object which should be compared with equals.
See What issues should be considered when overriding equals and hashCode in Java? for more details.