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 parse the process of assigning initial values to java during hashmap initialization

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

Share

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

How to analyze the process of assigning initial values to java during hashmap initialization, many beginners are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

HashMap in Java is a commonly used data structure, which is generally used as a container for data dictionary or Hash lookup.

In general, we initialize and assign initial values as follows:

HashMap map = new HashMap (); map.put ("name", "yanggb"); map.put ("lover", "huangq")

But sometimes we want to initialize and assign an initial value in an expression:

HashMap map = new HashMap () {{put ("name", "yanggb"); put ("lover", "huangq");}}

This is initialized with double parentheses [{{}}] to make the code simple and easy to read. The first bracket actually defines an anonymous inner class (Anonymous Inner Class), and the second bracket is actually an instance initialization block (Instance Initializer Block) that is executed when the internal anonymous class is constructed. The advantage of this method is obvious, that is, it is clear at a glance. However, this writing may lead to the failure of serialization of this object.

For one thing, because this is the declaration of an anonymous inner class, the reference holds a reference to the external class. So when serializing this collection, the external class will also be unknowingly serialized, and when the external class does not implement the Serialize interface, it will report an error.

Second, in the above example, a subclass inherited from HashMap is actually declared, but some serialization methods, such as serialization to json through Gson, or to serialization to xml, cannot serialize the subclass of Hashset or HashMap in the class library, which leads to serialization failure. The solution is to reinitialize to a HashMap object [new HashMap (map);] so that it can be initialized normally.

In addition, it should be noted that this syntax for initialization with double parentheses is slightly less efficient than the normal initialization method.

Finally, this syntax initialized with double parentheses also applies to collections such as ArrayList and Set.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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