hashCode
and equals
”. public class Main { public static void main(String[] args) { Object object = new Object(); int hCode; hCode = object.hashCode(); System.out.println(hCode); } }
int
, which is 4 bytes, and can put numbers from -2,147,483,648 to 2,147,483,647. At this stage it is important to understand that the hash code is a number that has its limit, which for java is limited to the primitive integer type int.derived from an array of arbitrary length.By an array of arbitrary length, we mean an object. In example 1, an object of the type
Object
is used as an array of arbitrary length.int
, and the set of objects is limited only by our imagination. Hence the following statement: “The set of objects is more powerful than the set of hash codes”. Due to this limitation, it is quite possible that the hash codes of different objects may coincide.equals()
methodnew
operator creates a new object in memory. To illustrate, create a class, let it be called “BlackBox”. public class BlackBox { int varA; int varB; BlackBox(int varA, int varB){ this.varA = varA; this.varB = varB; } }
BlackBox
. public class DemoBlackBox { public static void main(String[] args) { BlackBox object1 = new BlackBox(5, 10); BlackBox object2 = new BlackBox(5, 10); } }
Object
class, there is an equals()
method that compares the contents of objects and outputs a boolean true
value of boolean true
if the content is equivalent, and false
if not. object1.equals(object2);// true,
object1.equals(object2)// true object1.hashCode() == object2.hashCode()// true
false
. To clarify the reasons, let's look at the source code of the Object
class.Object
. In this class, the hashCode()
and equals()
methods are already defined.Object
class. And in a situation where your class does not override ( @overriding
) hashCode()
and equals()
, then their implementation from Object
.equals()
method in the Object
class. public boolean equals(Object obj) { return (this == obj); }
==
” operation returns true
only in one case - when the links point to the same object. In this case, the contents of the fields are not taken into account.equals
returns true
. public class DemoBlackBox { public static void main(String[] args) { BlackBox object3 = new BlackBox(5, 10); BlackBox object4 = object3;// object4 //- object3 object3.equals(object4)//true } }
Object.equals()
doesn’t work as it should, because it compares links, not the contents of objects.hashCode()
, which also does not work as it should.hashCode()
method in the Object
class: public native int hashCode();
native
means that the implementation of this method is performed in another language, for example, C, C ++ or assembler. The specific native int hashCode()
implemented in C ++, the source code is http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/tip/src/share/vm/runtime/synchronizer.cpp function get_next_hash
.Object
class, the Park-Miller RNG algorithm is used by default. The basis of this algorithm is a random number generator. This means that each time the program is started, the object will have a different hash code.hashCode()
method from the Object
class, we will get different hash codes each time we create an object of the new BlackBox()
class. Moreover, restarting the program, we will get completely different values, since this is just a random number.hashCode()
and equals()
methods so that the object fields are taken into account. public class BlackBox { int varA; int varB; BlackBox(int varA, int varB){ this.varA = varA; this.varB = varB; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + varA; result = prime * result + varB; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BlackBox other = (BlackBox) obj; if (varA != other.varA) return false; if (varB != other.varB) return false; return true; } }
hashCode()
and equals()
methods work correctly and take into account the contents of the object's fields: object1.equals(object2);//true object1.hashCode() == object2.hashCode();//true
hashCode()
and equals()
, so that they work correctly and take into account the data object. In addition, if you leave the implementation from Object
, then using java.util.HashMap
cause problems, since HashMap actively use hashCode()
and equals()
in their work, but it is well written about tarzan82 in the post Data structures in pictures. HashMap .Source: https://habr.com/ru/post/168195/
All Articles