Assigning values in Java when HASHMAP is initialized

Source: Internet
Author: User
Tags anonymous serialization

1, HashMap initialization of literary writing

HashMap is a commonly used data structure, usually used to do data dictionary or Hash lookup container. Ordinary youth would typically initialize this:
hashmap<string, string> map = new hashmap<string, string> ();
Map.put ("name", "Test");
Map.put ("Age", "20");


After reading this code, many people will feel that this is too verbose, literary youth generally come:
hashmap<string, string> map = new hashmap<string, string> () {
{
Map.put ("name", "Test");
Map.put ("Age", "20");
}
};


It looks elegant, one step, one go's bright. Then the question comes, what does the double-brace "{{}}" mean here, and what is the usage?
The double parenthesis "{{}}", which is used for initialization, makes the code simple and readable.
The first layer of parentheses actually defines an anonymous inner class (Anonymous Inner Class), and the second parenthesis is actually an instance initialization block (instance initializer block), which is executed when the inner anonymous class is constructed.


2, by and by, can initialize ArrayList, Set


list<string> names = new arraylist<string> () {
{
for (int i = 0; i < i++) {
Add ("A" + i);
}
}
};
System.out.println (Names.tostring ());


3, Java7: Increase the support of collections
In Java 7 You can create collections like Ruby, Perl, and Python, but these collections are immutable.


list<string> list = ["Item"];
String item = list[0];

Set<string> set = {"Item"};

map<string, integer> map = {"Key": 1};
int value = map["Key"];


4. Potential problems of literary writing


The advantages of literary writing mentioned at the beginning of the article are obvious. 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 anonymous internal classes are declared, 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 HashMap 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 HashMap object:


New HashMap (map);
This will allow the normal initialization of the.


5. Implementation efficiency issues


When a new tool or writing comes up, the apes will say, "How's the performance?" (This and boys talk about sister paper the first sentence is generally: "How long?" How much measurements? "One reason:)
About the two ways of writing on my side of the notebook to test the literary writing, the common way to create 10,000,000 Map results are 1217, 1064, the difference of 13%.
public class Test {
public static void Main (string[] args) {
Long st = System.currenttimemillis ();
/*
for (int i = 0; i < 10000000; i++) {
hashmap<string, string> map = new hashmap<string, string> () {
{
Put ("name", "Test");
Put ("Age", "20");
}
};
}
System.out.println (System.currenttimemillis ()-ST); 1217
*/


for (int i = 0; i < 10000000; i++) {
hashmap<string, string> map = new hashmap<string, string> ();
Map.put ("name", "Test");
Map.put ("Age", "20");
}
System.out.println (System.currenttimemillis ()-ST); 1064
}
}

Assigning values in Java when HASHMAP is initialized

Related Article

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.