Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
af78212
add->efficiency-of-java-double-brace-initialization.md
AcceptedBoy Aug 28, 2016
0ae910d
finished translating Why does this go into an infinite loop?
bboylin Aug 31, 2016
3105d5c
no message
0p3rator Sep 26, 2016
f2be3e8
first edition
Lianghui2 Sep 27, 2016
12c00b7
unfinished
Lianghui2 Sep 27, 2016
9c52fc0
first-commit
Lianghui2 Sep 28, 2016
3688c5e
第三篇
Lianghui2 Sep 29, 2016
8d0b2d8
第四篇
Lianghui2 Sep 30, 2016
d4cacdb
Update failed-to-load-the-JNI-shared-library(JDK).md
Lianghui2 Sep 30, 2016
fa4a973
Update failed-to-load-the-JNI-shared-library(JDK).md
Lianghui2 Sep 30, 2016
5e57dac
为什么不行
Lianghui2 Sep 30, 2016
d72929b
Update 备忘.md
Lianghui2 Sep 30, 2016
cc14bc8
第五篇
Lianghui2 Oct 2, 2016
54d129b
Merge branch 'master' of https://github.com/Lianghui2/stackoverflow-j…
Lianghui2 Oct 2, 2016
deb7717
第五篇
Lianghui2 Oct 2, 2016
ca3ff7f
woqu
Lianghui2 Oct 15, 2016
b58ab99
fix a typo
akuma Oct 20, 2016
941d6da
添加了<<当导入项目到eclipse时报错》的翻译
jinzhencheng Oct 30, 2016
62d40b1
update 'Sort ArrayList of custom Objects by property'
Oct 31, 2016
0e1260d
添加第一个翻译
lcs1795192 Jan 17, 2017
3fc6af0
更新了convert-a-string-to-an-enum-in-java,增加了一种常用回答
v2hoping Mar 5, 2017
7aa258e
Convert a String to an enum in Java 问题在一年前已被回答
v2hoping Mar 5, 2017
5e42d24
创建了apache-camel是什么的问题回答
v2hoping Mar 5, 2017
9ceec4f
Create why-does-math.round-(0.49999999999999994)-return-1.md
RWBUPT Mar 9, 2017
a58d15e
修改stackoverflow的链接
mysterin Apr 10, 2017
77a1c0f
Update iterate-through-a-hashmap.md
tuxiantian Jun 8, 2017
124eadd
look-enum-by-string-value.md 排版和更新
Sep 25, 2017
c24cd35
update README
Sep 25, 2017
d13b3b8
修复文档显示错误
CarGod Oct 17, 2017
8ddef1a
Update how-to-sort-a-mapkey-value-on-the-values-in-java.md
lidaobing Jul 3, 2018
79c54d9
Update README.md
lidaobing Jul 3, 2018
74b36bb
调整markdown格式,提高显示和阅读效果
razyer Aug 15, 2018
6e490df
Update how-can-i-initialize-a-static-map.md
jayknoxqu Oct 17, 2018
5eee13f
格式修改
indicolite Oct 26, 2018
b7d20aa
Create how-to-get-an-enum-value-from-a-string-value-in-java.md
245455758 Oct 28, 2018
cf30c73
初步完成了翻译
245455758 Oct 28, 2018
f01f980
Update how-to-get-an-enum-value-from-a-string-value-in-java.md
245455758 Oct 28, 2018
ca2b24d
创建文件
245455758 Oct 29, 2018
7500914
完成标题确定
245455758 Oct 29, 2018
6850f0c
Create how-do-i-decompile-java-class-files.md
withLiMin Apr 11, 2019
a2fe2a2
代码格式修复
Jisu-Woniu Apr 20, 2019
c14eac9
Merge pull request #106 from Jisu-Woniu/master
giantray Apr 24, 2019
9ca39a5
Merge pull request #105 from withLiMin/withlimin
giantray Apr 24, 2019
29d6d73
Merge pull request #76 from AcceptedBoy/master
giantray Apr 24, 2019
4f40d04
Merge pull request #77 from bboylin/master
giantray Apr 24, 2019
216ec85
Merge pull request #79 from 0p3rator/master
giantray Apr 24, 2019
d5b982c
Merge pull request #80 from Lianghui2/master
giantray Apr 25, 2019
f4a0667
Merge pull request #82 from akuma/patch-1
giantray Apr 25, 2019
f8e6098
Merge pull request #103 from 245455758/master
giantray May 10, 2019
d70e298
Merge pull request #101 from jayknoxqu/patch-1
giantray May 10, 2019
451cea2
Update java-operator.md
zsgwsjj May 14, 2019
84c0667
Update how-to-sort-a-mapkey-value-on-the-values-in-java.md
May 21, 2019
b6ef883
translation the question Why does this go into an infinite loop?
njustwh2014 May 27, 2019
d6425e8
Merge pull request #110 from njustwh2014/master
giantray Sep 14, 2019
060eeaa
Merge pull request #109 from Okeyja/patch-1
giantray Sep 14, 2019
e7d0bf2
Merge pull request #108 from zsgwsjj/patch-1
giantray Sep 14, 2019
a0c2dfe
Merge pull request #102 from indicolite/patch-1
giantray Sep 14, 2019
e351841
Merge pull request #100 from razyer/patch-1
giantray Sep 14, 2019
96aedc3
Merge pull request #99 from lidaobing/patch-2
giantray Sep 14, 2019
fee3a8d
Merge pull request #98 from lidaobing/patch-1
giantray Sep 14, 2019
dd738b3
Merge pull request #95 from CarGod/patch-1
giantray Sep 14, 2019
ee4db29
Merge pull request #92 from tuxiantian/patch-1
giantray Sep 14, 2019
2630b3e
Merge pull request #89 from v2hoping/patch-6
giantray Sep 14, 2019
b0adca9
Merge pull request #90 from RWBUPT/patch-2
giantray Sep 14, 2019
faf49a2
Merge pull request #83 from jinzhencheng/master
giantray Sep 14, 2019
ada8b32
Merge pull request #91 from mysterin/patch-1
giantray Sep 14, 2019
6778e5c
Merge pull request #84 from lcs1795192/patch-1
giantray Sep 14, 2019
08d195f
Merge pull request #88 from v2hoping/patch-2
giantray Sep 14, 2019
01b6166
Merge pull request #87 from v2hoping/patch-1
giantray Sep 14, 2019
9cc6dd8
Merge branch 'master' into Lookup-enum-by-string-value
giantray Sep 14, 2019
ba475c8
Merge pull request #94 from freezhan/Lookup-enum-by-string-value
giantray Sep 14, 2019
739fe6d
Merge branch 'smj' of https://github.com/AutumnLight/stackoverflow-ja…
giantray Sep 14, 2019
13a99f7
修复链接错误。close #97
giantray Sep 14, 2019
c35cced
更新readme
giantray Sep 14, 2019
926e85d
部分翻译没有加stackoverlow原文地址
giantray Sep 14, 2019
35dfc6f
更新readme
giantray Sep 14, 2019
db68803
Update README.md
giantray Sep 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ stackoverflow-Java-top-qa
对于参与翻译的人,这也是很好的一个学习、理解过程,欢迎大家一起来翻译

-------------
### 如何参与翻译(欢迎加入翻译组QQ群485011036)
####如何参与:
### 如何参与翻译
#### 如何参与:
- 请从下文“待翻译问题链接”中寻找你感兴趣的问答进行翻译。翻译好的问答,放到contents目录下,无需更新readme.md文档。之后提一个PR,我负责合并PR并更新到readme中。
- 另外,为了避免多人重复新翻译一个问题,你可以提issue,说明你计划翻译的问题及时间点,我可以先更新到下面的”未翻译问题“中,说明已有人领了这个问题。当然,也不一定要提issue,一般情况下,只要及时提pr,我及时审核,出现”撞车“的概率并不高。

####一些基本的约定:
#### 一些基本的约定:
- 文档的文件名,和stackoverflowhich-notnull-java-annotation-should-i-usew上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
- 在每篇翻译文档内容的最后,要附上stackoverflow的原文链接

####每个人可以做(但不限于):
#### 每个人可以做(但不限于):
- 找未翻译的问题进行翻译
- 优化已翻译的问题
- 输出问答精简汇总版(把所有的问答集中到一个md文件,然后尽量精简,让别人可以在一天内把这100个问题的精髓都看完)
- 输出gitbook版本(现在直接在github上查看,体验不好)

####文档优化反馈:
#### 文档优化反馈:
请大家多多反馈,优化已翻译好的文章:可以到[吐槽区](https://github.com/giantray/stackoverflow-java-top-qa/issues/66)吐槽,也可以在已翻译文章基础上进行优化,提新的PR。文章质量的提升,需要大家一起努力!


Expand Down Expand Up @@ -61,7 +61,6 @@ stackoverflow-Java-top-qa
* [为什么Java的```Vector```类被认为是过时的或者废弃的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-java-vector-class-considered-obsolete-or-deprecated.md)
* [Java的foreach循环是如何工作的](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-does-the-java-for-each-loop-work.md)
* [为什么相减这两个时间(1927年)会得到奇怪的结果](/contents/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result.md)
* [Java 中如何将 String 转换为 enum](/contents/lookup-enum-by-string-value.md)
* [该什么时候使用 ThreadLocal变量,它是如何工作的](/contents/when-and-how-should-i-use-a-threadlocal-variable.md)
* [servlets的运行原理](/contents/how-do-servlets-work-instantiation-shared-variables-and-multithreading.md)
* [如何计算MD5值](/contents/how-can-i-generate-an-md5-hash.md)
Expand All @@ -70,6 +69,10 @@ stackoverflow-Java-top-qa
* [Java内部类和嵌套静态类](/contents/java-inner-class-and-static-nested-class.md)
* [@Component, @Repository, @Service的区别](/contents/whats-the-difference-between-component-repository-service-annotations-in.md)
* [如何创建泛型java数组](/contents/how-to-create-a-generic-array-in-java.md)
* [如何在整数左填充0](/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
* [Java里什么是与C++的Pair<L,R>相等的?](/contents/what-is-the-equivalent-of-the-c++pair-in-java.md)
* [为什么数学函数Math.round(0.49999999999999994) 返回 1](/contents/why-does-math-round0-49999999999999994-return-1.md)
* [这段代码为什么陷入了死循环](/contents/why-does-this-go-into-an-infinite-loop.md)

> 编程技巧

Expand All @@ -92,9 +95,17 @@ stackoverflow-Java-top-qa
* [如何在整数左填充0](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md)
* [在调用 instanceof 前需要进行null检查吗](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-null-check-needed-before-calling-instanceof.md)
* [如何从文件里读取字符串](/contents/how-do-i-create-a-java-string-from-the-contents-of-a-file.md)
* [遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出](/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-reiterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md)
* [遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出](/contents/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re.md)
* [如何让IntelliJ编辑器永久性显示代码行数](/contents/how-can-i-permanently-have-line-numbers-in-intellij.md)
* [如何使用maven把项目及其依赖打包为可运行jar包](/contents/how-can-i-create-an-executable-jar-with-dependencies-using-maven.md)
* [重新导入项目到eclipse时遇到'Must Override a Superclass Method'报错](/contents/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips.md)
* [如何产生一个随机的字母数字串作为 session 的唯一标识符](/contents/how-to-generate-a-random-alpha-numeric-string.md)
* [Apache Camel是什么](/contents/what-exactly-is-apache-camel.md)
* [通过对象属性对常规对象的ArrayList进行排序](/contents/sort-arraylist-of-custom-objects-by-property.md)
* [为Eclipse自动代码格式化设置行的最大长度?](/contents/Eclipse-set-maximun-line-length-for-auto-formatting.md)
* [加载JNI共享库失败(JDK)](/contents/failed-to-load-the-JNI-shared-library(JDK).md)
* [如何对Java class文件进行反编译](/contents/how-do-i-decompile-java-class-files.md)
* [如何在classpath中设置多个jar包](/contents/setting-multiple-jars-in-java-classpath.md)

> 网络

Expand All @@ -107,6 +118,7 @@ stackoverflow-Java-top-qa
* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
* [如何使用Java创建一个内存泄漏的程序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/creating-a-memory-leak-with-java.md)
* [为什么打印“B”会明显的比打印“#”慢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-printing-b-dramatically-slower-than-printing.md)
* ["Double Brace Initialization"的效率问题](/contents/efficiency-of-java-double-brace-initialization.md)

> 测试

Expand All @@ -118,34 +130,21 @@ stackoverflow-Java-top-qa
* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md)
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
* [安装Android SDK的时候找不到JDK](contents/android-sdk-installation-doesnt-find-jdk.md)
* [安卓中“UserManger.isUserAGoat()”的合适案例](contents/proper-use-cases-for-android-usermanager-isuseragoat.md)

### 待翻译问题链接(还剩x问题)
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)

### 待翻译问题链接(还剩 13 问题)
- [Why is executing Java code in comments with certain Unicode characters allowed?](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed)
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
- [Access restriction on class due to restriction on required library rt.jar?](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar)
- [How do I discover memory usage of my application in Android?](http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android)
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
- [Can I add jars to maven 2 build classpath without installing them?](http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them)
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
- [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath)
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
- [How can I pad an integers with zeros on the left?](http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left)
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
- [Efficiency of Java “Double Brace Initialization”?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
- [decompiling DEX into Java sourcecode](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode)
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
- [What is the equivalent of the C++ Pair<L,R> in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
- [How do I “decompile” Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
- [Useful Eclipse Java Code Templates [closed]](http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates)
- [How to call SOAP web service in Android](http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-in-android)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
为自动代码调整设置最大的行数?
问题:我正在学习Java。如果我在Eclipse Helios里使用ctrl+shift+f的组合键,它会自动调整我的代码。一定程度下,它会改变行数。我想增加行数的最大值。应该怎么做?

回答,在偏好设置里,分别点击Java->Code Style->Fomatter->edit,在菜单栏Line Wrapping下会有行的宽度选择(Maximun line width).你将需要编辑你的代码轮廓。

stackoverflow原址:http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting
2 changes: 1 addition & 1 deletion contents/avoiding-null-statements-in-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ ParserFactory.getParser().findAction(someInput).doSomething();
- 如果你想返回null,请停下来想一想,这个地方是否更应该抛出一个异常

stackoverflow链接:
http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java?page=2&tab=votes#tab-top
http://stackoverflow.com/questions/271526/avoiding-null-statements?page=1&tab=votes#tab-top
34 changes: 33 additions & 1 deletion contents/convert-a-string-to-an-enum-in-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ Enum.valueOf()是否能实现以上目的,如果是,那我如何使用?

### 其他答案

当文本和枚举值不同时,可以采用这种方式:
```java
public enum Blah {
A("text1"),
B("text2"),
C("text3"),
D("text4");

private String text;

Blah(String text) {
this.text = text;
}

public String getText() {
return this.text;
}

public static Blah fromString(String text) {
for (Blah b : Blah.values()) {
if (b.text.equalsIgnoreCase(text)) {
return b;
}
}
return null;
}
}
```
fromString方法中,throw new IllegalArgumentException("No constant with text " + text + " found") 会比直接返回null更优秀.

### 其他答案

我有一个挺赞的工具方法:
```java
/**
Expand Down Expand Up @@ -47,4 +79,4 @@ public static MyEnum fromString(String name) {
}
```

stackoverflow链接:http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
stackoverflow链接:http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
202 changes: 202 additions & 0 deletions contents/efficiency-of-java-double-brace-initialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
## "Double Brace Initialization"的效率问题

#### 问题

`Double Brace Initialization`是java的隐藏特性,它有着如下诱人的语法:

```java
Set<String> flavors = new HashSet<String>() {{
add("vanilla");
add("strawberry");
add("chocolate");
add("butter pecan");
}};
```

但是,这个特性听说不是很高效率,是否要限制一次性使用?

#### 回答

当我使用匿名内部类时出现了如下的问题:

```Auto
2009/05/27 16:35 1,602 DemoApp2$1.class
2009/05/27 16:35 1,976 DemoApp2$10.class
2009/05/27 16:35 1,919 DemoApp2$11.class
2009/05/27 16:35 2,404 DemoApp2$12.class
2009/05/27 16:35 1,197 DemoApp2$13.class

/* snip */

2009/05/27 16:35 1,953 DemoApp2$30.class
2009/05/27 16:35 1,910 DemoApp2$31.class
2009/05/27 16:35 2,007 DemoApp2$32.class
2009/05/27 16:35 926 DemoApp2$33$1$1.class
2009/05/27 16:35 4,104 DemoApp2$33$1.class
2009/05/27 16:35 2,849 DemoApp2$33.class
2009/05/27 16:35 926 DemoApp2$34$1$1.class
2009/05/27 16:35 4,234 DemoApp2$34$1.class
2009/05/27 16:35 2,849 DemoApp2$34.class

/* snip */

2009/05/27 16:35 614 DemoApp2$40.class
2009/05/27 16:35 2,344 DemoApp2$5.class
2009/05/27 16:35 1,551 DemoApp2$6.class
2009/05/27 16:35 1,604 DemoApp2$7.class
2009/05/27 16:35 1,809 DemoApp2$8.class
2009/05/27 16:35 2,022 DemoApp2$9.class
```

这是在我的一个简单应用中所产生的类信息。在这个应用中,使用了大量的匿名内部类,这些类会被单独地编译成`class`文件。

`Double Brace Initialization`是一个带有实例初始化块的匿名内部类。这就意味着每一个新的类的产生都会执行一次实例块,这样的目的通常是为了创建一个简单的对象。

java虚拟机在使用类之前需要去读取其classes信息,然后执行字节码校验等流程。所以为了保存这些`class`文件,所需要的磁盘空间会增大。

这个可以说是`Double Brace Initialization`的开销。所以尽量不要过分使用。

---

在java的介绍中,`Double Brace Initialization`有着如下的写法:

```java
List<String> list = new ArrayList<String>() {{
add("Hello");
add("World!");
}};
```

看起来像是java的隐藏特性,其实它只是下面代码的一个重写:

```java
List<String> list = new ArrayList<String>() {

// 实例初始化块
{
add("Hello");
add("World!");
}
};
```

所以,它只是在匿名内部类中加上了实例初始化块而已。

---

Joshua Bloch希望以后的集合代码能够像这样简介:

```java
List<Integer> intList = [1, 2, 3, 4];

Set<String> strSet = {"Apple", "Banana", "Cactus"};

Map<String, Integer> truthMap = { "answer" : 42 };
```

但目前还没有这样的语法。

---

<strong>实践</strong>

做一个简单的试验:创建1000个带着`"Hello"`和`"World!"`元素的`ArrayList`

* 方法1:Double Brace Initialization

```
List<String> l = new ArrayList<String>() {{
add("Hello");
add("World!");
}};
```

* 方法2:初始化`ArrayList`并调用`add`方法

```java
List<String> l = new ArrayList<String>();
l.add("Hello");
l.add("World!");
```

我修改了java的源文件使之能够为每种上述方法分别创建出1000个实例

* 方法1的测试

```java
class Test1 {
public static void main(String[] s) {
long st = System.currentTimeMillis();

List<String> l0 = new ArrayList<String>() {{
add("Hello");
add("World!");
}};

List<String> l1 = new ArrayList<String>() {{
add("Hello");
add("World!");
}};

/* snip */

List<String> l999 = new ArrayList<String>() {{
add("Hello");
add("World!");
}};

System.out.println(System.currentTimeMillis() - st);
}
```

* 方法2的测试

```java
class Test2 {
public static void main(String[] s) {
long st = System.currentTimeMillis();

List<String> l0 = new ArrayList<String>();
l0.add("Hello");
l0.add("World!");

List<String> l1 = new ArrayList<String>();
l1.add("Hello");
l1.add("World!");

/* snip */

List<String> l999 = new ArrayList<String>();
l999.add("Hello");
l999.add("World!");

System.out.println(System.currentTimeMillis() - st);
}
}
```

然后得出了下面的测试时间:

```Auto
Test1 Times (ms) Test2 Times (ms)
---------------- ----------------
187 0
203 0
203 0
188 0
188 0
187 0
203 0
188 0
188 0
203 0
```

从上面我们可以看到,`Double Brace Initialization`平均时间花费了190ms左右。
同时,另外一种方法平均只用了0ms。

所以,在第一个方法测试程序中,即`Double Brace Initialization`产生了1000个`class`文件。

## StackOverflow地址

[http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
Loading