| |

VerySource

 Forgot password?
 Register
Search
View: 2857|Reply: 19

The role of hashCode ()

[Copy link]

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-5 12:20:01
| Show all posts |Read mode
Will the master:
1. What does hashcode () do?
2. If I implement the comparable interface and override equals (), why should I override hashCode ()? Is the default equals () better than hashCode? The hashCode is calculated based on the memory address, but two instances of a class are placed in different address spaces in the heap area. Why is the calculated hash code the same?
3. Is compareTo () in the comparable interface used for system callbacks to implement custom sorting? What about compare () in the Comparator interface? What are their similarities and differences? When does the system call back the comparare method?

Looking at the JDK, I'm in the fog. I was puzzled, and I hope any kind person can give me a pointer or two, and thank you here!
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-8 22:09:01
| Show all posts
The hashcode method returns the hash code value of the object. This method is supported to provide some advantages to hash tables, such as the hash table provided by java.util.Hashtable.

The general contract of hashCode is:
During the execution of a Java application, when the hashCode method is called multiple times on the same object, the same integer must be consistently returned, provided that the information used in the equals comparison on the object has not been modified. The integer need not be consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals (Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
The following is not necessary: ​​if two objects are not equal according to the equals (java.lang.Object) method, then calling the hashCode method on either of the two objects will necessarily produce a different integer result. However, programmers should be aware that generating different integer results for unequal objects can improve hash table performance.
In fact, the hashCode method defined by the Object class does return different integers for different objects. (This is typically achieved by converting the internal address of the object to an integer, but the JavaTM programming language does not require this implementation technique.)

When the equals method is overridden, it is usually necessary to override the hashCode method to maintain the general contract of the hashCode method, which states that equal objects must have equal hash codes.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-9 09:09:02
| Show all posts
Can you say your opinion upstairs!
Is it useful to paste all the original text in the Chinese API?
Just look at the documentation and don't understand it! You have all of them pasted again!
Reply

Use magic Report

1

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Invalid IP Address

Post time: 2020-1-9 17:00:01
| Show all posts
Just
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-10 09:54:01
| Show all posts
1.hashcode is used to find, if you have learned the data structure, you should know that in the chapter of find and sort
Such as in memory
0 1 2 3 4 5 6 7
And I have a class, this class has a field called ID, I want to store this class in one of the above 8 locations, if you do n’t store it arbitrarily, then you need to go to each of these eight locations when searching , Or use algorithms such as dichotomy.
But if you use hashcode, it will improve the efficiency a lot.
There is a field in our class called ID, so we define our hashcode as ID% 8, and then store our class in the position where the remainder is obtained. For example, if our ID is 9, and the remainder of 9 divided by 8 is 1, then we store the class at position 1. If the ID is 13, and the remainder is 5, then we put the class at position 5. In this way, when searching for this class, you can directly find the storage location by dividing the ID by 8 and finding the remainder.

2. But what if the two classes have the same hashcode (we assume that the ID of the above class is not unique), for example, the remainder of 9 divided by 8 and 17 divided by 8 is 1, then this is not legal, The answer is: yes. So how to judge? At this point you need to define equals.
In other words, we first use hashcode to determine whether two classes are stored in a bucket, but there may be many classes in this bucket, then we need to find the class we want in this bucket by using equals.
Well. Overriding equals (), why should we overwrite hashCode ()?
Think about it, if you want to find something in a bucket, you must first find the bucket. You don't find the bucket by rewriting hashcode (). What's the use of rewriting equals ()?
3. There are two ways to sort Class A. One is to let Class A implement the comparabole structure and implement the compareTo () method. Then you can sort them by Collections.sort (List <A> list)
Another method: define a class B to implement the Comparator class and implement the compare method,
Then sort by Collections.sort (List <A> list, B b)
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-10 22:00:01
| Show all posts
hashCode () is used to generate a hashma, and the hashma is used to determine the storage address of the object in the hash storage structure. The collection classes with hash all use this storage structure: HashMap, HashSet, when they store objects (strictly speaking, object references), they need to determine their address, and HashCode () is for this purpose, generally You need to redefine it, because by default, the hashCode method defined by the Object class returns different integers for different objects. This is generally achieved by converting the internal address of the object into an integer. Now for example In terms of HashSet, when an object is stored in it, the hashCode () of the object is stored to determine the storage address of the object in the HashSet, and equals () is used to determine whether the stored object is duplicated, hashCode (), Equals () need to be redefined by themselves, because hashCode () has already been said by default, and equals () is a comparison object reference by default. Now you think about it, If you do not define equals (), then two objects with the same content generated by the same class can be stored in the Set, because they are determined by equals (), which makes the HashSet lose its meaning. See Take a look at this:
import java.util. *;
class A
{
private int i;
public A (int i) {
this.i = i;
}
public String toString () {
return "" + i;
}
public boolean equals (Object o) {
A a = (A) o;
return (a.i == i)? true: false;
}
public int hashCode () {
return i;
}


}

public class TestA
{
public static void main (String [] args) {
HashSet set = new HashSet ();
for (int i = 0; i <= 3; i ++)
set.add (new A (i));
System.out.println (set);
set.add (new A (1));
System.out.println (set);
System.out.println (set.contains (new A (0)));
System.out.println (set.add (new A (1)));
System.out.println (set.add (new A (4)));
System.out.println (set);


}

}
You can comment out hashCode () and equals () respectively to compare their functions and you can pull them. The key is to see the comparison results yourself and you can remember them clearly.
Reply

Use magic Report

3

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-17 15:00:01
| Show all posts
mark
Reply

Use magic Report

0

Threads

39

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-18 11:54:01
| Show all posts
FYI: http://blog.csdn.net/axman/category/64977.aspx
Reply

Use magic Report

3

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-19 11:54:01
| Show all posts
Hashcode must be used in conjunction with equals, and the performance of HashMap depends on them
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-19 13:54:01
| Show all posts
My exams are all from Google ..
Hashcode and equals are binding relationships, and are generally not commonly used.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list