By chance, I saw a way to initialize an object:
// 新建一个列表,并赋值 "Harry","Tony","Tom" ArrayList<String> friends = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); }};
Of course, the same initialization method is also used for the Map collection:
// 新建一个Map,并赋值 Map<String, Object> cat = new HashMap<String, Object>() {{ put("name", "Tom"); put("age", 10); }};
Here, internal class syntax is used, this method It is much more convenient and concise than adding new objects first and then adding them one after another. This method is called "double brace initialization" (double brace initialization).
Take the initialization of ArrayList as an example. The first layer of curly braces first defines an anonymous internal class inherited from ArrayList
ArrayList<String> friends = new ArrayList<String>() { // 这里什么操作都没有,全部继承自父类(ArrayList) };
The second layer is a custom object construction block (called a non-static initialization block)
new ArrayList<String>() { // 这里什么操作都没有,全部继承自父类(ArrayList) };
We get the instantiation of the ArrayList subclass through new, and then up-cast it into a reference to ArrayList
ArrayList<String> friends = new ArrayList<String>() {{}};
The friends we get are actually references to subclasses of ArrayList, but there is no change in function
Compared to the regular standard way of initialization It is much simpler (but the readability of the code will be relatively poor)
Using double braces to initialize a collection may not be as efficient as the standard collection initialization step . The reason is that using double curly braces for initialization will lead to the generation of internal class files, and this process will affect the execution efficiency of the code.
First check the .class files generated by different initialization methods
For example, the following code:
public class Test1 { public static void main(String[] args) { System.out.println(System.currentTimeMillis()); ArrayList<String> list1 = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); add("Jerry"); }}; ArrayList<String> list2 = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); add("Jerry"); }}; ArrayList<String> list3 = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); add("Jerry"); }}; ArrayList<String> list4 = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); add("Jerry"); }}; ArrayList<String> list5 = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); add("Jerry"); }}; …… …snip… …… ArrayList<String> list1000 = new ArrayList<String>() {{ add("Harry"); add("Tony"); add("Tom"); add("Jerry"); }}; System.out.println(System.currentTimeMillis()); } }
The .class list generated after compilation of Test1 is:
Only 1 .class file is generatedRunning timeTest1$1.class
Test2.class
Test1$2.class
Test1$3.class
Test1$4.class
Test1$5.class
……
…snip…
... :
The first piece of code Test1 runs Result:
1508379452224
1508379452784The running time is: 560 milliseconds
The second code Test2 running result:15083796715071508379671505
The running time is: 2 milliseconds
Although this time gap will vary depending on the computer performance and operating status, it can also be explained The double brace initialization method takes longer than the conventional method1. The double brace initialization method generates more .class files than the conventional method2. The double brace initialization method takes longer to run than the conventional method
To sum up, (when the test initialization data is less (the list has not reached the auto-increment critical point)) the double brace initialization method is less efficient than the conventional method :
The above is the detailed content of How to use double brace initialization in Java. For more information, please follow other related articles on the PHP Chinese website!