Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use hashCode method in Java

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use the hashCode method in Java". In the daily operation, I believe that many people have doubts about how to use the hashCode method in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the hashCode method in Java". Next, please follow the editor to study!

1. Introduce the hashCode method

The purpose of hashCode () is to get a hash code, also known as a hash code, which actually returns an int integer. The purpose of this hash code is to determine the index position of the object in the hash table.

HashCode () is defined in JDK's Object.java, which means that any class in Java contains the hashCode () function.

The hash table stores key-value pairs (key-value), which is characterized by the ability to quickly retrieve the corresponding "value" according to the "key". This is the use of hash codes! You can quickly find the object you need.

2. Why do you need the hashCode method?

In the process of writing programs, it is a very common and common problem to judge whether two objects are the same or not. The hashCode () method is used to speed up the comparison of two objects.

Let's take "how HashSet checks for duplicates" as an example to illustrate why there is hashCode:

When you add an object to HashSet, HashSet will first calculate the hashcode value of the object to determine the location of the object, and will also compare the hashcode value of other objects that have been added. If there is no matching hashcode, HashSet will assume that the object does not repeat.

But if an object with the same hashcode value is found, the equals () method is called to check whether the object with equal hashcode is really the same. If the two are the same, HashSet will not let it join successfully. If it is different, it will be hashed back to another location.

In this way, we greatly reduce the number of equals, and correspondingly greatly improve the execution speed.

3. What is the relationship between hashCode () and equals ()?

Java defines the eqauls () method and the hashCode () method as follows:

The hashCode () method is called multiple times on the same object, always returning the same integer value.

If a.equals (b), then there must be a.hashCode () must be equal to b.hashCode ().

If! a.equals (b), then a.hashCode () is not necessarily equal to b.hashCode (). At this point, if a.hashCode () is always not equal to b.hashCode (), it will improve the performance of hashtables.

A.hashCode () = b.hashCode (), then a.equals (b) can be true or false.

A.hashCode ()! = b.hashCode (), then a.equals (b) is false.

A brief note of the above conclusions:

If two objects equals,Java the runtime environment will assume that their hashCode must be equal.

If two objects are not equals, their hashCode is likely to be equal.

If two objects are equal in hashCode, they are not necessarily equals.

If the hashCode of two objects is not equal, they must not be equals.

4. Why do I have to override the equals method override the hashcode method?

We explained above that if two objects equals, then their hashCode values must be equal. If only the equals method is rewritten, but not the hashCode method, the value of the hashCode will be different, and the result determined by the equals method is true.

In some containers in Java, two identical objects are not allowed, and when inserting, if the judgment is the same, it will be overwritten. At this point, if only the method of equals is overridden, but not the method of hashCode, hashCode in Object is a hash value based on the translation of the storage address of the object. At this time, it is possible to cause the problem that the object cannot be overridden because the hashCode method is not overridden, resulting in the same object being hashed to different locations.

For example

Dog class

Package com.xiao;/** * @ author: Xiao Xiao * @ date: Created in 14:42 on 2022-3-11 * / public class Dog {private String name; private Integer age; public Dog () {} public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age } public void setAge (Integer age) {this.age = age;} public Dog (String name, Integer age) {this.name = name; this.age = age;} @ Override public boolean equals (Object obj) {if (obj.getClass ()! = getClass ()) {return false;} Dog dog = (Dog) obj If (dog.getAge () = = age & & dog.getName () .equals (name)) {return true;} return false;}}

Test class

Import com.xiao.Dog;public class Test {public static void main (String [] args) {Dog dog = new Dog ("Xiaowang", 2); Dog dog1 = new Dog ("Xiaowang", 2); System.out.println ("equals result:" + dog.equals (dog1)); System.out.println ("whether the hashCode value of dog is equal to the hashCode value of dog1:" + (dog.hashCode () = = dog1.hashCode ());}}

Test result

Equals result: true

Whether the hashCode value of dog is equal to the hashCode value of dog1: false

At this point, the study on "how to use the hashCode method in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report