Equals and hashcode methods
- Eclipse: Oxygen
- Java: 1.8
In this tutorial, we will learn “Equals and hashcode methods”. The equals method is used to compare two objects whether they have the same characteristics. This is done by comparing two hashcodes of the object which is also explained in this tutorial. The hashcode is like an id, each element has a unique id based on their declarations hence two same datatypes having the same content have the same hashcode. This way the equals to methods word.
In the following program, check out the result when “= =” (equal to equal to) and equals method is used to compare string data.
“= =” (equal to equal to) method returns a boolean value after completing the operation.
1 2 3 4 5 6 7 8 9 10 |
public class App { public static void main(String[] args) { String name1 = "Chaand"; String name2 = "Chaand"; System.out.println(name1== name2); } } |
Output
true
“= =” (equal to equal to) method gives incorrect result when string data created with the help of object of string class is compared.
1 2 3 4 5 6 7 8 9 |
public class App { public static void main(String[] args) { String name1 = new String("chaand"); String name2 = new String("chaand"); System.out.println(name1== name2); } } |
Output
false
So when we deal with String object comparison, it is recommended to use equals() method.
1 2 3 4 5 6 7 8 9 |
public class App { public static void main(String[] args) { String name1 = new String("chaand"); String name2 = new String("chaand"); System.out.println(name1.equals(name2)); } } |
Output
true
When it comes to comparing the custom objects equals() methods do not work correctly. It is always recommended to overwrite the hashcode() method when custom objects are compared.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
class Code implements Comparable<Code>{ private String sectionNo; private String lectureNo; public Code(String sectionNo, String lectureNo) { super(); this.sectionNo = sectionNo; this.lectureNo = lectureNo; } public String getSectionNo() { return sectionNo; } public String getLectureNo() { return lectureNo; } @Override public String toString() { return "Code [sectionNo=" + sectionNo + ", lectureNo=" + lectureNo + "]"; } @Override public int compareTo(Code o) { String code1 = sectionNo.concat(lectureNo); String code2 = o.getSectionNo()+o.getLectureNo(); return code1.compareTo(code2); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((lectureNo == null) ? 0 : lectureNo.hashCode()); result = prime * result + ((sectionNo == null) ? 0 : sectionNo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Code other = (Code) obj; if (lectureNo == null) { if (other.lectureNo != null) return false; } else if (!lectureNo.equals(other.lectureNo)) return false; if (sectionNo == null) { if (other.sectionNo != null) return false; } else if (!sectionNo.equals(other.sectionNo)) return false; return true; } } public class App { public static void main(String[] args) { Map<Code,String> lectures = new TreeMap<>(); lectures.put(new Code("S01","L03"), "Generics"); lectures.put(new Code("S01","L01"), "Files under Java"); lectures.put(new Code("S02","L03"), "Network programming"); lectures.put(new Code("S01","L07"), "OOPS"); lectures.put(new Code("S01","L05"), "Methods"); lectures.put(new Code("S01","L03"), "Expressions"); for(Map.Entry<Code, String> entry: lectures.entrySet()){ // System.out.println("Key: "+entry.getKey()+" Value: "+entry.getValue()); } Code code1 = new Code("S01","L03"); Code code2 = new Code("S01","L03"); System.out.println(code1.equals(code2)); } } |
Output
true
Contributed by: Poonam Tomar