In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to rewrite haseCode and equals in HashSet". In daily operation, I believe many people have doubts about how to rewrite haseCode and equals in HashSet. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to rewrite haseCode and equals in HashSet". Next, please follow the editor to study!
In order to save an uncertain amount of data, as well as data with mapping relationships, Java provides collection classes
The Set in the collection, like a large jar, can store one or more objects in the Set collection in turn. The Set collection usually cannot remember the order in which elements are added.
However, it is not allowed to include two identical elements in the Set collection to join the same Set collection. When multiple threads access the same HashSet at the same time, when multiple threads modify the HashSet collection at the same time, the synchronization must be ensured through code.
So how does HashSet distinguish between different elements? This brings us to hashCode and equals.
When an element is added to the HashSet collection, HashSet will call the hashCode method of the object to get the hashCode value of the object, and then determine the position of the object in the HashSet according to the hashCode value. If the two elements return true through the comparison of the equals method, but their hashCode method returns different values, HashSet will store them in different locations and can still be successfully added.
That is to say, HashSet distinguishes whether the element is the same or not by judging the equality by equals and comparing the hashcode value.
Let's look at a piece of code.
Class h {public boolean equals (object obj) {return true;}} class z {public int hashCode () {return 1;}} class t {public int hashCode () {return 2;} public boolean equals (object obj) {return true;}} public class HashSetTest {public static void main (String [] args) {var b=new HashSet (); b.add (new h); b.add (new h); b.add (new z) B.add (new z); b.add (new t); b.add (new t); System.out.println (b);}}
The result is:
Hobbies 5674ddrec hobbies 0987as records1 pencils 1 pencils 1 pencils 2
That is to say,
Two h objects return true by comparing them with the equals method, but HashSet still treats them as two objects
Two z objects return the same value through hashCode, but HashSet still treats them as two objects
Two t objects return true by comparing them with the equals method, and the same value is returned by hashCode. Only then does HashSet treat them as one object.
Visible: when you put an object in HashSet, if you rewrite equals, you should rewrite hashCode.
When equals compares that two objects are equal, if the hashCode is different, two identical elements will be added to the HashSet, which conflicts with the rules of the Set collection.
At this point, there is an extreme situation: what if the hashCode is equal and the equals is different?
Then, it will be more troublesome. HashSet tried to put them together, but he couldn't.
Therefore, the chain structure will be adopted to save multiple objects, which in turn affects performance and leads to performance degradation.
At this point, the study on "how to rewrite haseCode and equals in HashSet" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.