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

HashMap stores custom classes, and which methods of custom classes need to be overridden

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "HashMap storage custom classes, need to rewrite custom classes of which methods", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "HashMap storage custom classes, need to rewrite custom classes of which methods" it!

Override the hashcode and equals methods.

The hashCode () method is a local native method that returns the memory address of the object stored in the object reference, while the equals method is compared with = =, which is also the memory address of the object.

Key in HashMap is compared in this way, first find out the hashcode () of Key, compare whether it is equal, compare equals (), if they are equal, they are considered to be equal. If you only override hashcode () and not equals (), their memory addresses are compared when equals () is called. So both methods must be rewritten together.

Thank you for your reading, the above is the content of "HashMap stores custom classes, which methods of custom classes need to be rewritten". After the study of this article, I believe you have a deeper understanding of the problem that HashMap stores custom classes and which methods need to be rewritten. Specific usage also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report