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 de-duplicate Custom objects by HashSet Collection in Java

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to de-duplicate custom objects in the HashSet collection in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The Set interface in Java is a subinterface of Collectio, and the Set collection is not allowed to contain the same elements. If you add the same element, add () returns FALSE, and the new element is not added. Set collections are commonly used for numeric elements, string deduplication, etc., but when the element is a custom object type, is Set deduplication as we expected? The following will take HashSet as an example, through a series of experiments to verify step by step.

1. First set up a class of FootBallPlayer football players

two。 (suppose: HashSet will identify objects with the same attribute values as duplicates.) in order to test whether the de-duplication effect of HashSet on the object is consistent with the guess, we first build three object instances, among which two "Ronaldo" with the same attributes are constructed.

The result: HashSet does not identify two "Ronaldo" objects as duplicates, and all three instances are added to the HashSet collection.

3. Before you look at how HashSet is de-duplicated, take a look at how HashSet is implemented. By looking at the JDK source code, it is found that HashSet is actually operating on HashMap.

4. If you continue to look at the add () method of hashSet, you are actually calling the put () method of HashMap

5. Continue tracking until the putVal () method (focus)

Looking carefully at the putVal () method, it is found that whether the new element is repeated or not is based on the following two kinds of criteria.

To determine whether the hash value is equal, both by judging the hashCode () method

To determine whether it is equal, through the equals () method

6. After understanding the two judgment conditions, we first do a simple experiment by calling the equals () method of objects such as Integer, String, Object and so on.

It turns out that the value returned by the custom Object object equals is false. Next, let's look at their equals implementation one by one.

(1) the equals implementation of the Integer object is found to be based on whether the values are equal or not by reading the code.

(2) the equals implementation of String object is based on judging whether the referenced object is the same or not, and then comparing the value of its string one by one.

(3) the judgment of Object is based on whether the referenced object is the same. Since the above two football players are from the new new and are not the same object, the result returned by equlas () is false.

7. After seeing the implementation of equlas, let's take a look at the hashCode implementation of Integer String Object. Also do a simple test first, call their hashCode () method to calculate the hash value and compare it.

The result of the experiment is that the hash values of the two Object objects are not equal. Let's take a look at their implementation of hashcode ().

(1) through the source code to find that Integer is through its value value to enter the operation line to get the hash value.

(2) String also calculates the hash value by calculating its value value, so the result in the test is true

(3) when looking at the hashCode () method of Object, it is found that there is no specific implementation. By consulting the data, we know that the calculation of the default hashCode of JDK8 is handed over to C++. The method is through a random number + three definite values related to the current thread, using Marsaglia's.

A random number obtained by xorshifschema random number algorithm. So two different objects get different hash values, and the test result is false. (for Object's hashCode (), there is no in-depth discussion here. Friends who know too much are welcome to share.)

8. Knowing that HashSet is deduplicated through hashcode () and equals (), and the implementation principle of equals () and hashcode () of custom Object object, then to implement HashSet according to our expectation, when the values of all properties of the two objects are the same, we can rewrite the equals () and hashcode () of the FootBallPlayer class as follows.

HashCode () is rewritten as a hash value by calculating the values of all the properties of the object.

Equals () is rewritten to first determine whether the referenced object is the same, and then determine whether each attribute value of the object is equal.

9. After rewriting the method, we re-execute the original program, which is the same three examples of football players. As expected, HashSet deduplicated the "Ronaldo" object.

Summary

The bottom layer of HashSet is the operation of HashMap, and its de-weight principle determines whether it is repeated by the methods of hashCode () and equals (). Through experiments, it is found that the reason why the custom object is not deduplicated successfully is related to the implementation of JDK default Object object hashCode () and equals (). For the deduplication of a custom object, we can override the hashCode () and equals () of the custom object to make it follow the rules we want.

This is the end of the content of "how to deduplicate custom objects in the HashSet collection in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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