Java double bracket initialization operation Technique _java

Source: Internet
Author: User
Tags new set

Because the Java language's collection framework (collections, such as list, map, set, and so on) does not provide any simple grammatical structure, this makes the work of creating constant collections very cumbersome. Every time we build it, we have to do it:

Define a temporary collection class variable to create an instance of an empty collection, and then assign the value to the variable to put the data into the collection and then pass the collection as a parameter to the method

For example, to pass a set variable to a method:

Set validcodes = new HashSet ();
Validcodes.add ("xz13s");
Validcodes.add ("ab21/x");
Validcodes.add ("Yylex");
Validcodes.add ("ar2d");
Removeproductswithcodein (Validcodes);

You can also use the static initial method

private static final Set validcodes = new HashSet ();
static {
Validcodes.add ("xz13s");
Validcodes.add ("ab21/x");
Validcodes.add ("Yylex");
Validcodes.add ("ar2d");
}
private static final Set validcodes = new HashSet ();
static {
Validcodes.add ("xz13s");
Validcodes.add ("ab21/x");
Validcodes.add ("Yylex");
Validcodes.add ("ar2d");
}

In fact, there are also simple knot methods, we can use the double bracket syntax (Double-brace syntax) to establish and initialize a new set:

private static final Set valid_codes = new HashSet () {
Add ("xz13s");
Add ("ab21/x");
Add ("Yylex");
Add ("ar2d");
};
private static final Set valid_codes = new HashSet () {
Add ("xz13s");
Add ("ab21/x");
Add ("Yylex");
Add ("ar2d");
};

Or

Removeproductswithcodein (New HashSet () {
Add ("xz13s");
Add ("ab21/x");
Add ("Yylex");
Add ("ar5e");
});
Removeproductswithcodein (New HashSet () {
Add ("xz13s");
Add ("ab21/x");
Add ("Yylex");
Add ("ar5e");
});

The first bracket actually defines an internal anonymous class (Anonymous Inner Class), and the second bracket is actually an instance initialization block (instance initializer block), which is executed when the internal anonymous class is constructed. This block is called "instance initialization blocks" because they are defined within the scope of an instance of a class. This differs from the static initialization block, which is defined by using the static keyword before parentheses, so that its and class are in the same range, that is, when the class is loaded, it is executed as a initialzer.

The instance initialization block can use all the methods and variables within its container scope, but it is particularly noteworthy that the instance initialization block is run before the constructor.

This approach applies only to classes that are not final, because the final class cannot establish an internal anonymous subclass, but the collection class has no such restriction. Therefore, this method can also be used to initialize any other object, such as a GUI object:

Add (New JPanel () {{
setlayout (...));
SetBorder (...);
Add (New JLabel (...));
Add (New JSpinner (...)
); Add (New JPanel () {{
setlayout (...));
SetBorder (...);
Add (New JLabel (...));
Add (New JSpinner (...));

This creates an instance of the internal anonymous class that packages the reference to the image in its container. If serialized (serialization) This collection will also serialize its inner class.

In addition, this kind of double bracket initialization method for map initialization can reduce a lot of code, see:

 map<string, set<string>> baselinemap = new hashmap<> ();
      Final String schema = "schema";
      if (baselinemap.get (type) = = null) {
        baselinemap.put (type, new hashset<string> () {
          Add (schema);
        }}) ;
      } else {
        baselinemap.put (type, new hashset<string> (Baselinemap.get (type)) {{
          Add (schema);}}
        );
      

This code I believe that without special explanation, basic can understand, think of how many lines of code to write if do not use the method of double bracket initialization! ~

This is about Java double bracket initialization techniques, improve the readability of the code, and simplify the number of code, you can try to apply to their own projects.

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.