一equals
##The equals method is Object-level. By default, it compares the memory addresses of two objects. Many classes have overridden this method to compare the actual contents of the objects. , generally compare whether the attribute values of the same attributes of the same type of objects are the same.
2 hashCode
1. Hash table
The hash table is an array, Each element in the array is a one-way linked list. The nodes in the same one-way linked list have the same attributes. The attribute value is the subscript of the array. This same attribute is called hashCode hash value. .
2. The function and significance of hashCode hash value
Before hashCode is generated, the retrieval set needs to be compared one by one, which results in low query efficiency. If elements with the same attribute value in a collection are divided into a group based on a certain attribute, is stored centrally. When querying, the value of the attribute of the query object is first obtained, and the corresponding group is located based on the value, which greatly reduces the problem. It increases the query scope and improves the query efficiency. This is the background of hashCode.
The hashCode of two objects is equal, which only indicates that they are in the same group. It is not guaranteed to be the same. You must also compare the memory addresses of the two objects or pass ## The #equals method compares the specific contents. Only when this item is satisfied can it be determined that the two objects are the same.
It can be seen from the above that in the hash table, the same two objects must have the same hash value.3.hashCode() method
The hashCode() method is Object-level and by default generates an int value based on the memory address of the object. This value has a one-to-one correspondence with the memory address. Since the same two objects are confirmed by equals
only to ensure that the attribute values of the same attributes are the same, without rewriting the hashCode, the hash values are different and are stored in are treated as two objects with different contents in the hash table, resulting in two elements with the same content appearing in the hash table, occupying unnecessary memory. Therefore, once the equals method is rewritten, it must be rewritten. Write the hashCode method and ensure that the same objects have equal values.
Three applicationsHash values are mainly used in hash tables. The bottom layer of HashMap and HashSet is a hash table, so if you want to use When an object is stored in a HashMap or HashSet,
must override the equals and hashCode methods in the class so that the same objects have equal hash values.
The above is the detailed content of Summary of the usage of equals and hashCode. For more information, please follow other related articles on the PHP Chinese website!

Using POI library in Java to add borders to Excel files Many Java developers are using Apache...

Efficient processing of batch interface requests: Using CompletableFuture to ensure that concurrent calls to third-party interfaces can significantly improve efficiency when processing large amounts of data. �...

In JavaWeb applications, the feasibility of implementing entity-class caching in Dao layer When developing JavaWeb applications, performance optimization has always been the focus of developers. Either...

The current status of motorcycle and motorcycle systems and ecological development of motorcycle systems, as an important bridge connecting knights and vehicles, has developed rapidly in recent years. Many car friends...

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)