Problems with the Java map double-brace initialization method

Source: Internet
Author: User
Tags object serialization

About the initialization of the Java double brackets is usually very convenient, especially in the constant file, there is no substitute. As shown below:

Map map = new HashMap () {

Put ("Name", "Unmi");  
Put ("QQ", "1125535");  

};

The benefits are clear at a glance. Here's a list of the disadvantages of this approach, and if the object is to be serialized, it can cause serialization to fail.

1. This is the way the anonymous inner class is declared (not understood below for a detailed explanation), so references to external classes are held in the reference. Therefore, the serialization of this set when the external class will also be unknowingly serialized, when the external class does not implement the Serialize interface, will be error.

2. In the above example, a subclass that inherits from HashSet is actually declared. However, some serialization methods, such as to serialize to JSON via Gson, or to serialize to XML, are provided in a class library that cannot serialize HashSet or HashMap subclasses, resulting in serialization failures. Workaround: Reinitialize to a HashSet object:

New HashMap (map);

This will allow the normal initialization of the.

The principle of two-bracket notation:

The first layer of parentheses actually defines an internal anonymous class (Anonymous Inner Class), and the second layer of parentheses is actually an instance initialization block (instance initializer block), which is executed when the inner anonymous class is constructed. This block is called "instance initialization blocks" because they are defined within the scope of an instance of a class.

If the above code is written in the Testdoublebrace class, after compiling you will see that the Testdoublebrace$1.class file is generated and the contents of the file are deserialized:

Final class Com.unmi.testdoublebrace$1 extends java.util.hashmap{//creates a subclass of HashMap testdoublebracke$1

Com.unmi.testdoublebrace$1 ();

Code: 
0:aload_0 
1:invokespecial #8; Method java/util/hashmap. "":() The code in V//{} has been put in the construction method. 
4:aload_0 
5:LDC #10; String Name 
7:LDC #12; String Unmi 
9:invokevirtual #14; Method put: (Ljava/lang/object; Ljava/lang/object;) Ljava/lang/object; 
12:pop 
13:aload_0 
14:LDC #18; String QQ 
16:LDC #20; String 1125535 
18:invokevirtual #14; Method put: (Ljava/lang/object; Ljava/lang/object;) Ljava/lang/object; 
21:pop 
22:return 
}

151 recommendations for writing high-quality code to improve Java programs PDF HD full version http://www.linuxidc.com/Linux/2014-06/103388.htm

Java 8 Concise tutorial http://www.linuxidc.com/Linux/2014-03/98754.htm

Simple validation of the initialization order of Java objects http://www.linuxidc.com/Linux/2014-02/96220.htm

Summary of Java object value passing and object passing http://www.linuxidc.com/Linux/2012-12/76692.htm

Java object Serialization ObjectOutputStream and ObjectInputStream example http://www.linuxidc.com/Linux/2012-08/68360.htm

This article permanently updates the link address : http://www.linuxidc.com/Linux/2014-06/103535.htm

Problems with the Java map double-brace initialization method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.