| |

VerySource

 Forgot password?
 Register
Search
Author: wccheese

The role of hashCode ()

[Copy link]

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-20 10:09:02
| Show all posts
The friend upstairs spoke very thoroughly, but I still have some doubts:
1. Equals () compares references to the 8 big wrapper classes and String classes, compared to references. When comparing these 9 classes,
The comparison is not a reference, but a conversion to the corresponding basic type (except String-> char []).
(By the way: Why doesn't StringBuffer override equals ()?)

Here is my understanding:
If equals () is not overridden, the comparison between objects will be based on the memory address ratio, so that any instance of any class is not possible
equals () returns true.
If equals () is overridden, it is compared with its own defined method. At this time, even without overriding hashCode (),
The correct comparison result is obtained, but the hashCode returned at this time is different (the same possibility is very small), if you only want
It ’s not much to do anything else (for fun), at this time hashCode is not important to us, but: as the friend upstairs said: When you want to use a hash data structure to store and retrieve data, all It is operated through hashCode. If the hash code is different, then equal () is not needed. If the hash code is the same, then equals () is better than it. ) Is true, but there will still be two or more references to such objects in the Set (because hashCode is different). Another extreme case:
Rewriting hashCode () but not equals () will cause multiple references to the same instance of the same class to be added to the Set. Instances of different classes may not be added because the hashCode is the same (although the probability is crazy) ).
(By the way: how to calculate the hash code of an object in JAVA, isn't it calculated based on the memory address? The memory address is unique, and why may there be duplicate hash codes?)
Don't know even if I understand? I also ask experts to give pointers to younger brothers.

2. The compare method in the Comparator interface produces a comparator, which has a higher comparison level than compareTo () rewritten in the Comparable interface. Is this correct?

Thanks again!
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-20 22:54:01
| Show all posts
1.hashCode () method is used to improve the search efficiency in Map.Map will be placed in different buckets according to different hashCode (). When searching for an object, Map first finds the corresponding bucket through hashCode (), and then Then find the corresponding object according to the equals () method. To correctly implement the search for elements in the Map, two conditions must be met:
(1) When obj1.equals (obj2) is true, obj1.hashCode () == obj2.hashCode () must be true
(2) obj.equals (obj2) must be true when obj1.hashCode ()! = Obj2.hashCode () is true

2.A class implementing the Comparable interface is used to determine the natural order of objects of this class.In this way, when placing this class of objects in a TreeSet or TreeMap, they will be automatically sorted according to the natural order of this class of objects. Will sort according to the natural order of the objects

3. The implementation of Comparable can only determine the natural order of this class of objects, but many times need custom sorting rules, then we must implement
Comparator interface.For example, a People implements the Comparable interface and defines the natural order as alphabetical order by name.When storing People in a List, you can use the Collections.sort () method to sort the List according to the natural order.The problem is that What should I do to sort People by age? A class that implements Comparable can only override the compareTo () method once, then there can only be one order-natural order, but in order to sort this List by age size , You must use a class to implement Comparator and override the compare () method, define the rules for sorting by age, and then call static <T> void sort (List <T> list, Comparator <? Super T> c) of Collections To sort the list in the order specified by the specified comparator
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-21 09:18:01
| Show all posts
The above conditions are written a bit wrong, it should be
(1) When obj1.equals (obj2) is true, obj1.hashCode () == obj2.hashCode () must be true
(2) When obj1.hashCode () == obj2.hashCode () is false, obj.equals (obj2) must be false
Reply

Use magic Report

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-2-5 01:30:01
| Show all posts
Thank you for your guidance, benefit a lot, thank you very much, very much !!
Reply

Use magic Report

1

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Invalid IP Address

Post time: 2020-2-7 20:00:01
| Show all posts
Good post
!!!!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 10:00:01
| Show all posts
mark
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 12:15:01
| Show all posts
Pick up
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 15:45:01
| Show all posts
Good post
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-3 00:30:01
| Show all posts
Can not Understand it! Still not clear!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-3 01:00:01
| Show all posts
Good post
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