+ 1

Can anyone explain why the two overrides are done what is their purpose and explain its wprking

class Animal { String name; Animal(String n) { name = n; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.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; Animal other = (Animal) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Program { public static void main(String[ ] args) { Animal a1 = new Animal("Robby"); Animal a2 = new Animal("Robby"); System.out.println(a1.equals(a2)); } }

9th May 2019, 3:41 PM
rishabh tesla
rishabh tesla - avatar
4 Respostas
+ 3
You can read this article about the hashCode() method: https://www.baeldung.com/java-hashcode Each Object has an equals method. For example String.equals(); And each object has a hashCode() method which returns an Integer (value from the hash algorithm). If you compare two objects and the equals method returns true then they must have the same hash code. A HashSet or HashMap uses the hash codes to store the elements. (That's why it is not possible to store equals objects). If you want, you can do this: String str = "hello"; String str2 = "hello"; System.out.println(str.equals(str2)); System.out.println(str.hashCode()); System.out.println(str2.hashCode()); In your example you have an own class Animal with the objects a1 and a2. I make an example where you can see what happens if you don't override hashCode() and equals(). https://code.sololearn.com/cA9FZ5Yh9QI4/?ref=app I hope this helps to understand why you need to override theese methods.
13th May 2019, 3:53 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Every class automatically inherits from the Object-class. Object-class includes the methods overriden here. hashCode() generates a hash of a class instance so that it can be stored into a hashmap or hashset for example. equals() checks if two instances of a class are equal. I haven't looked it up but I guess that equals() uses the hashes (generated by hasCode) of those two objects. Sometimes you want/need to implement those methods differently and have to override them in the custom class you have built. In Python (but I guess it will be similar for Java) the corresponding method (it is called __hash__ there) will not work most of the time on your custom class instance and just return that the instance is not hashable. These are the cases where you definitely need to implement the hashCode-method yourself or you won't be able to store them in containers which require a hash value. Hth!
9th May 2019, 4:08 PM
Thoq!
Thoq! - avatar
0
Hi rishabh tesla, In this example, it's all about the equals() method. The purpose of this class is to create Animal objects which can be compared to other Animal objects. If you override the equals() method in Java, then you always have to override hashCode too! The @Override annotation indicates that the methods already exist in the 'Object' class (Every class extends this class) and that you are replacing your code with the premade Objects-code. In this example, you want to override the equals method since you're assuming that two animals with the same name are equal. The hashCode() makes sure that two 'identical' objects have the same code when theyre 'hashed'. Usually you can achieve this using a multiplier of 31 and hashcodes of the class variables you use in the equals() method. I hope I have answered your question. Let me know if you have any further questions :). Robbe
9th May 2019, 7:50 PM
Robbe Nooyens
Robbe Nooyens - avatar
0
It looks like a big bunch of implemented bullshit. Get HashCode and Equals are abstract members of the base class object. First is used to define a member (value) based identifier for your object (not necessarily unique), but that is used to determine existence within datastructures as dictionaries or hashsets. Equals let you define a value based equality function used in comparer.
9th May 2019, 9:40 PM
Daniel Adam
Daniel Adam - avatar