j2ee Advanced Development Technology Course Week 8

Introduction 1.

 

 The functions of the hashCode() method and the equal() method are actually the same. In Java , they are used to compare whether two objects are equal or not. Then, since equal() can already achieve the function of comparison, why do you need hashCode()?

Because the comparison in the rewritten equal() is generally more comprehensive and complex, so the efficiency is relatively low, and using hashCode() for comparison, you only need to generate a hash value for comparison, and the efficiency is very high, then hashCode() Since the efficiency is so high, why do you need equal()?

Because hashCode() is not completely reliable, sometimes the hashcodes generated by different objects will be the same (there may be problems in generating hash value formulas), so hashCode() can only be said to be reliable most of the time, not absolutely reliable. So we can get:

         1. Two objects that are equal() are equal, their hashCode() must be equal, that is, the comparison with equal() is absolutely reliable.

         2. Two objects with equal hashCode() are not necessarily equal in their equal(), that is, hashCode() is not absolutely reliable.

It is obviously too inefficient to use equal() for all the comparisons that require a lot of speed and speed, so the solution is to use hashCode() to compare first whenever you need to compare. If the hashCode() is different, then Indicates that the two objects are definitely not equal (that is, there is no need to use equal() to compare again). If the hashCode() is the same, then compare their equal() at this time. If equal() is also the same, it means that the two The objects are really the same, which can greatly improve the efficiency and ensure the absolute correctness of the comparison! 

     This kind of large and fast object comparison is generally used in hash containers, such as hashset, hashmap, hashtable, etc. For example, if objects in hashset cannot be repeated, he must compare each object added internally, and he The comparison rule is as mentioned above, first hashCode(), if the hashCode() is the same, then use equal() to verify, if the hashCode() is different, it must be different, so the comparison efficiency is very high.

      However, hashCode(), like equal(), is a method in the basic class Object, and like equal(), hashCode() in Object just returns the address of the current object. If this is the case, then we are the same class, Two new objects, because their addresses in memory are different, their hashCode() is different, so this is obviously not what we want, so we must rewrite the hashCode() method of our class, that is, a class, in hashCode () returns a unique hash value, such as the following:

customize a class 

class Person{

   int num;

   String name;

 

   public int hashCode(){

      return num*name.hashCode();

}

Since this class is identified by its internal variables num and name, we return a hash value based on them as the unique hash value of this class. 

So if our object wants to be put into hashSet and play the characteristics of hashSet (that is, it does not contain the same object), then we have to rewrite our class's hashCode() and

equal() method. These two methods have been rewritten internally in classes like String, Integer, etc.

 

Of course, if we just want to compare whether two objects are consistent, we can only rewrite one equal(), and then use equal() to compare.

 

Introduction 2.

HashCode

The basis for the generation of hash codes: Hash codes are not completely unique. It is an algorithm that allows objects of the same class to have different hash codes according to their different characteristics, but does not indicate different object hashes. code is completely different. There is also the same situation, see how the programmer writes the algorithm of the hash code.

 

What is HashCode

In Java, hash codes represent characteristics of objects.
For example, the object String str1 = "aa", str1.hashCode= 3104
String str2 = “bb”, str2.hashCode= 3106
String str3 = “aa”, str3.hashCode= 3104
According to HashCode, it can be concluded that str1!=str2, str1==str3
Several commonly used hash code algorithms are given below.
1: The hashCode of the Object class. Returns the processed structure of the memory address of the object. Since the memory address of each object is different, the hash code is also different.
2: The hashCode of the String class. According to the content of the string contained in the String class, the hash code is returned according to a special algorithm. As long as the heap space where the string is located is the same, the returned hash code is also the same.
3: Integer class, the returned hash code is the value of the integer contained in the Integer object, for example, Integer i1=new Integer(100), the value of i1.hashCode is 100. It can be seen that two Integer objects of the same size return the same hash code.

The application of equals method in hibernate.

The equals method is the default method for judging whether two objects are equal. It is implemented in the Object class and judges the memory addresses of the two objects. In hibernate, it is not allowed to have 2 identical instances of the same object. Hibernate judges by the equals method. Such as:
User u1 = new User("Zhang San");
User u2 = new User("Li Si");
User u3 = new User("Zhang San");
According to the project requirements, as long as the user has the same name, it means the same user, so we believe that u1 and u3 are the same person and the same object. But because the memory addresses of u1, u2, and u3 are all different, hibernate will think that these are 3 different objects. This contradicts our assumption. Therefore, we will override the equals method in the Object class.
public class User{
private String userName;
....//get, set method saves
//Override the equals method in Object
public boolean equals(Object arg0){
if (!(arg0 instanceof User)){
return false;
}
User user = (User)arg0;
//If the names are the same, it means they belong to the same object.
if(user.getName().equals(this.getName)){
return true;
}else{
return false; }
}
In this way, when hibernate inserts data, if a user named "Zhang San" is passed, hibernate will first determine whether there is a user named "Zhang San". This can ensure a high degree of data consistency. Different projects have different requirements, so you need to override the equals method according to your own needs.

The relationship between equals and HashCode

In hibernate, it thinks that as long as equals returns true, the hashCode must be equal for two objects. But what about the actual situation?
User u1 = new User("Zhang San");
User u2 = new User("Zhang San");
Since we have rewritten the equals method of User, u1.equals(u2); returns true. However, User does not rewrite the hashCode method, it uses the hashCode method of the Object class, so u1.hashCode = 31050006 u2.hashCode = 31587890 The hashCodes of the two are not equal. Violating the principle of hibernate, hibernate will make a wrong judgment, and think that they are not the same object, so we have to rewrite the hashCode method of User. How to override the hashCode method?

Rewrite of HashCode

As mentioned in Section 2, in order to accomplish such a thing with hash code, we must first ensure that if the results from equals are equal, then the hashCode is also equal. Like the above u1 and u2, since the names are both "Zhang San", the same hashCode should be returned. So we can think of a way. Let User's hash code return the hash code of the name field in User, so as to ensure that people with the same name not only have the same equals method, but also have the same hashCode. Then the User class becomes
public class User{
private String userName;
//Override the equals method in Object
public boolean equals(Object arg0){
if(!(arg0 instanceof User)){
return false;
}
User user = (User)arg0;
//If the names are the same, it means they belong to the same object.
if (user.getName().equals(this.getName)){
return true;
}else{
return false;
}
}
//Override the hashCode method in Object
public int hashCode() {
return name.hashCode();//Return the hash code of the name.
}
}
This ensures that hibernate judges duplicate objects according to our own needs

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325618502&siteId=291194637