Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from giantray:master #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 165 commits into reborn2013:master from giantray:master
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
165 commits
Select commit Hold shift + click to select a range
342ea67
Create why-is-printing-b-dramatically-slower-than-printing.md
zhongjianluxian Apr 4, 2016
e0961b7
Merge pull request #1 from zhongjianluxian/why-is-printing-b-dramatic...
zhongjianluxian Apr 4, 2016
92e3f02
remove unnecessary spaces
zhongjianluxian Apr 4, 2016
8ca6028
Merge pull request #2 from zhongjianluxian/why-is-printing-b-dramatic...
zhongjianluxian Apr 4, 2016
dd3587c
Update why-is-printing-b-dramatically-slower-than-printing.md
zhongjianluxian Apr 5, 2016
ae67302
Merge pull request #3 from zhongjianluxian/why-is-printing-b-dramatic...
zhongjianluxian Apr 5, 2016
463f633
Create how-can-i-pad-an-integers-with-zeros-on-the-left.md
zhongjianluxian Apr 5, 2016
79a74a6
java的vector类为什么被认为过时了
jssyjam Apr 17, 2016
9f94d69
Update why-is-java-vector-class-considered-obsolete-or-deprecated.md
jssyjam Apr 17, 2016
951f880
fanyi
jssyjam Apr 17, 2016
95d58f5
xiugai
jssyjam Apr 17, 2016
c223730
Create onvert-a-string-to-an-enum-in-java.md
tangculijier May 3, 2016
5d872de
Create how-does-the-java-for-each-loop-work.md
alexduan-mel May 9, 2016
eae0527
Update what-is-a-serialversionuid-and-why-should-i-use-it.md
KingAlen May 18, 2016
413b445
tianjiafanyi
May 19, 2016
f82979f
Update what-issues-should-be-considered-when-overriding-equals-and-ha...
NothingOne May 19, 2016
c59b831
Merge pull request #40 from zhongjianluxian/master
giantray May 23, 2016
e4c99d7
更新已翻译问题
giantray May 23, 2016
b49f2a4
Merge branch 'master' of https://github.com/jssyjam/stackoverflow-jav...
giantray May 23, 2016
2012c1d
Merge branch 'jssyjam-master'
giantray May 23, 2016
6173090
更新未翻译问题
giantray May 23, 2016
ee5bfae
no message
giantray May 23, 2016
d4c89a5
Merge pull request #47 from wjk20120522/patch-1
giantray May 24, 2016
f092ace
Merge pull request #50 from NothingOne/patch-1
giantray May 24, 2016
016ec72
Merge pull request #46 from ArronDon/master
giantray May 24, 2016
36e774a
Merge pull request #44 from tangculijier/master
giantray May 24, 2016
7ab16f3
更新已翻译问题
giantray May 25, 2016
f0bc018
Merge branch 'master' of https://github.com/YuxiangQue/stackoverflow-...
giantray May 25, 2016
a4adac5
更新已翻译问题
giantray May 25, 2016
c4ef2b4
Why is subtracting these two times (in 1927) giving a strange result?
severalfly May 27, 2016
3a6e5f3
Merge pull request #51 from severalfly/master
giantray May 28, 2016
4164f04
代码排版、修改错别字
Yixiaohan May 31, 2016
6c3adf5
Merge pull request #52 from Yixiaohan/patch-3
giantray Jun 1, 2016
4b61f49
修改提交空文件的问题
Jun 1, 2016
3711480
Merge pull request #1 from giantray/master
alexduan-mel Jun 1, 2016
94d6991
when-and-how-should-i-use-a-threadlocal-variable
alexduan-mel Jun 1, 2016
36c07fc
Update when-and-how-should-i-use-a-threadlocal-variable.md
alexduan-mel Jun 1, 2016
500ee1d
翻译Convert a String to an enum in Java
DaiDongLiang Jun 30, 2016
cc2ca4a
Create why-is-subtracting-these-two-times-in-1927-giving-a-strange-re...
gaoshihang Jul 25, 2016
bbe5225
Update why-is-subtracting-these-two-times-in-1927-giving-a-strange-re...
gaoshihang Jul 25, 2016
a914c65
owen1190
owen1190 Aug 2, 2016
7c7b598
更新已翻译问题
giantray Aug 2, 2016
8101202
Merge pull request #49 from zouzls/master
giantray Aug 2, 2016
cbc23f1
Merge pull request #53 from ArronDon/master
giantray Aug 2, 2016
1192526
Merge pull request #54 from DaiDongLiang/master
giantray Aug 2, 2016
0c8be56
更新已翻译问题
giantray Aug 2, 2016
dd26ebd
Merge branch 'master' of https://github.com/andysim3d/stackoverflow-j...
giantray Aug 2, 2016
9eafdcb
更新已翻译问题
giantray Aug 2, 2016
df120f9
Merge pull request #57 from gaoshihang/patch-2
giantray Aug 2, 2016
b609912
更新已翻译问题
giantray Aug 2, 2016
2d7c9a0
添加gitbook
giantray Aug 3, 2016
561f48d
owen1190 8/3
owen1190 Aug 3, 2016
465351b
translate how-do-i-create-a-java-string-from-the-contents-of-a-file
andysim3d Aug 4, 2016
b11f01e
Update how-do-i-create-a-java-string-from-the-contents-of-a-file.md
andysim3d Aug 4, 2016
1eeda60
Update how-do-i-create-a-java-string-from-the-contents-of-a-file.md
andysim3d Aug 4, 2016
e719fac
what-is-the-difference-between-a-soft-reference-and-a-weak-reference-...
owen1190 Aug 5, 2016
dfc8923
what-is-the-difference-between-a-soft-reference-and-a-weak-reference-...
owen1190 Aug 5, 2016
cbbb387
what-is-the-difference-between-jsf-servlet-and-jsp finished
Aug 9, 2016
14b2cc9
Merge pull request #61 from owen1190/master
giantray Aug 17, 2016
984c94b
更新已翻译问题
giantray Aug 17, 2016
38708d6
Merge pull request #63 from andysim3d/translate_create_string_from_file
giantray Aug 17, 2016
0edbc18
更新已翻译问题
giantray Aug 17, 2016
b940fe1
Merge pull request #64 from owen1190/master
giantray Aug 17, 2016
155499c
Merge pull request #65 from ccrfrog/master
giantray Aug 17, 2016
679cce1
更新已翻译问题
giantray Aug 17, 2016
1d7bcfa
更新吐槽方式
giantray Aug 17, 2016
8cc8fe4
ADD: java-inner-class-and-static-nested-class.md
troyliu0105 Aug 17, 2016
63083bf
owen1190
owen1190 Aug 17, 2016
9e9aa6e
add->whats-the-difference-between-component-repository-service-annota...
AcceptedBoy Aug 17, 2016
1b93836
Merge pull request #67 from troyliu0105/master
giantray Aug 17, 2016
13fd9c6
更新已翻译问题
giantray Aug 17, 2016
c5fb142
Merge pull request #68 from owen1190/master
giantray Aug 17, 2016
60e629a
Merge pull request #70 from AcceptedBoy/master
giantray Aug 17, 2016
8fffae4
更新已翻译问题
giantray Aug 17, 2016
8561923
add->iterating-through-a-collection-avoiding-concurrentmodificationex...
AcceptedBoy Aug 17, 2016
174194e
Merge pull request #72 from AcceptedBoy/master
giantray Aug 18, 2016
5ebf821
更新已翻译问题
giantray Aug 18, 2016
6280541
add->how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-...
AcceptedBoy Aug 18, 2016
01ca924
update->junit4 && add->create_generic_array
AcceptedBoy Aug 18, 2016
6aaae68
add_stackoverflow_address->create_generic_array
AcceptedBoy Aug 18, 2016
86bf225
add->how-can-i-create-an-executable-jar-with-dependencies-using-maven.md
AcceptedBoy Aug 18, 2016
4631058
add->intellij && update->difference of spring annotation
AcceptedBoy Aug 18, 2016
1bac5e9
android-sdk-installation-doesnt-find-jdk
Aug 22, 2016
ac2cf11
Merge pull request #73 from AcceptedBoy/master
giantray Aug 22, 2016
bca7ea7
更新已翻译问题
giantray Aug 22, 2016
463b264
Merge pull request #75 from NoahZu/master
giantray Aug 22, 2016
f54a51d
更新已翻译问题
giantray Aug 22, 2016
ca5550b
更新已翻译问题
giantray Aug 24, 2016
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
93 changes: 48 additions & 45 deletions README.md
View file Open in desktop

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions SUMMARY.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\# Summary* [前言](README.md)
View file Open in desktop
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
33 changes: 33 additions & 0 deletions contents/android-sdk-installation-doesnt-find-jdk.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##安装Android SDK的时候找不到JDK

###问题
我在我的win7 64位的系统上安装Android SDK时,jdk-6u23-windows-x64.exe已经安装上了,但是Android SDK的安装程序却因为找不到已安装的JDK无法继续下去。
这个问题出现过吗?有没有办法解决呢?

![](http://ww2.sinaimg.cn/large/0060lm7Tgw1f72ny3m6oaj30ds0a0gmi.jpg)

###回答1:
当你看到这个提示(找不到jdk)的时候按Back(返回),然后再点Next(下一步)。这个时候,它将会去寻找JDK

###回答2:
实际安装:

- 系统:windows 8.1
- JDK文件: jdk-8u11-windows-x64.exe
- ADT文件:installer_r23.0.2-windows.exe
安装64位JDK,然后尝试第一个回答中的back-next的方法。然后尝试设置JAVA_HOME 根据错误信息的提示,但是,仍旧对我没有用处,然后,尝试如下解决办法:

按照它说的做,设置JAVA_HOME在你的系统环境变量中,这个路径要使用正斜杠(/)而非反斜杠(\)

**注意:**
当我把JAVA_HOME设置为C:\Program Files\Java\jdk1.6.0_31的时候还是不行,但是当我设置成C:/Program Files/Java/jdk1.6.0_31的时候就ok了。快把我逼疯了。

如果还不行,就把 %JAVA_HOME%加在环境变量Path的头部。

下面是我的环境变量的配置:
- JAVA_HOME=C:/Program Files/Java/jdk1.8.0_11
- JRE_HOME=C:/Program Files/Java/jre8
- Path=%JAVA_HOME%;C:...

stackoverflow链接:
http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk
16 changes: 9 additions & 7 deletions contents/avoiding-null-statements-in-java.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public interface Parser {

我们来改造一下

类定义如下,这样定义findAction方法后,确保无论用户输入什么,都不会返回null对象
类定义如下,这样定义findAction方法后,确保无论用户输入什么,都不会返回null对象:
```java
public class MyParser implements Parser {
private static Action DO_NOTHING = new Action() {
public void doSomething() { /* do nothing */ }
Expand All @@ -62,7 +63,7 @@ public class MyParser implements Parser {
return DO_NOTHING;
}
}}

```

对比下面两份调用实例
1. 冗余: 每获取一个对象,就判一次空
Expand All @@ -74,7 +75,8 @@ if (parser == null) {
}
Action action = parser.findAction(someInput);
if (action == null) {
// do nothing} else {
// do nothing}
else {
action.doSomething();}
```

Expand All @@ -90,11 +92,11 @@ ParserFactory.getParser().findAction(someInput).doSomething();
- 如果要用equal方法,请用object<不可能为空>.equal(object<可能为空>))
例如:
使用
"bar".equals(foo)
`"bar".equals(foo) `
而不是
foo.equals("bar")
`foo.equals("bar") `
- Java8或者guava lib中,提供了Optional类,这是一个元素容器,通过它来封装对象,可以减少判空。不过代码量还是不少。不爽。
- 如果你想返回null,请挺下来想一想,这个地方是否更应该抛出一个异常
- 如果你想返回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
10 changes: 5 additions & 5 deletions contents/comparing-java-enum-members-or-equals.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public final boolean equals(Object other) {
答案是肯定的,因为枚举有着严格的实例化控制,所以你可以用 == 去做比较符,这个用法,在官方文档中也有明确的说明。

>JLS 8.9 Enums
>An enum type has no instances other than those defined by its enum constants.
>It is a compile-time error to attempt to explicitly instantiate an enum type. The final clone method in Enum >ensures that enum constants can never be cloned, and the special treatment by the serialization mechanism ensures >that duplicate instances are never created as a result of deserialization. Reflective instantiation of enum types >is prohibited. Together, these four things ensure that no instances of an enum type exist beyond those defined by >the enum constants.
>Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the >equals method when comparing two object references if it is known that at least one of them refers to an enum ?>constant. (The equals method in Enum is a final method that merely invokes super.equals on its argument and ?>returns the result, thus performing an identity comparison.)
一个枚举类型除了定义的那些枚举常量外没有其他实例了。
试图明确地说明一种枚举类型是会导致编译期异常。在枚举中final clone方法确保枚举常量从不会被克隆,而且序列化机制会确保从不会因为反序列化而创造复制的实例。枚举类型的反射实例化也是被禁止的。总之,以上内容确保了除了定义的枚举常量之外,没有枚举类型实例。

因为每个枚举常量只有一个实例,所以如果在比较两个参考值,至少有一个涉及到枚举常量时,允许使用"=="代替equals()。(equals()方法在枚举类中是一个final方法,在参数和返回结果时,很少调用父类的equals()方法,因此是一种恒等的比较。)
#### 什么时候 == 和 equals 不一样?
As a reminder, it needs to be said that generally, == is NOT a viable alternative to equals. When it is, however (such as with enum), there are two important differences to consider:
通常来说 == 不是一个 equals的一个备选方案,无论如何有2个重要的不同处需要考虑:
Expand All @@ -73,8 +73,8 @@ if (Color.BLACK == Chiral.LEFT); // DOESN'T COMPILE!!! Incompatible types!
Bloch specifically mentions that immutable classes that have proper control over their instances can guarantee to their clients that == is usable. enum is specifically mentioned to exemplify.
具体来说,那些提供恰当实例控制的不可变类能够保证 == 是可用的,枚举刚好符合这个条件。

> Item 1: Consider static factory methods instead of constructors
[...] it allows an immutable class to make the guarantee that no two equal instances exist: a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients can use the == operator instead of the equals(Object) method, which may result in improved performance. Enum types provide this guarantee.
考虑静态工厂方法代替构造器
它使得不可变的类可以确保不会存在两个相等的实例,即当且仅当a==b的时候才有a.equals(b)为true。如果类保证了这一点,它的客户端可以使用"=="操作符来代替equals(Object)方法,这样可以提升性能。枚举类型保证了这一点

总而言之,在枚举比较上使用 == , 因为:
1. 能正常工作
Expand Down
34 changes: 33 additions & 1 deletion contents/convert-a-string-to-an-enum-in-java.md
View file Open in desktop
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
View file Open in desktop
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 DemoApp21ドル.class
2009年05月27日 16:35 1,976 DemoApp210ドル.class
2009年05月27日 16:35 1,919 DemoApp211ドル.class
2009年05月27日 16:35 2,404 DemoApp212ドル.class
2009年05月27日 16:35 1,197 DemoApp213ドル.class

/* snip */

2009年05月27日 16:35 1,953 DemoApp230ドル.class
2009年05月27日 16:35 1,910 DemoApp231ドル.class
2009年05月27日 16:35 2,007 DemoApp232ドル.class
2009年05月27日 16:35 926 DemoApp233ドル1ドル1ドル.class
2009年05月27日 16:35 4,104 DemoApp233ドル1ドル.class
2009年05月27日 16:35 2,849 DemoApp233ドル.class
2009年05月27日 16:35 926 DemoApp234ドル1ドル1ドル.class
2009年05月27日 16:35 4,234 DemoApp234ドル1ドル.class
2009年05月27日 16:35 2,849 DemoApp234ドル.class

/* snip */

2009年05月27日 16:35 614 DemoApp240ドル.class
2009年05月27日 16:35 2,344 DemoApp25ドル.class
2009年05月27日 16:35 1,551 DemoApp26ドル.class
2009年05月27日 16:35 1,604 DemoApp27ドル.class
2009年05月27日 16:35 1,809 DemoApp28ドル.class
2009年05月27日 16:35 2,022 DemoApp29ドル.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)
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

### [装饰模式](http://en.wikipedia.org/wiki/Decorator_pattern)

- [java.io.InputStream](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html),[OutputStream](http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html),[Reader](http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html) 和 [Writer](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html) 的所有资料都有一个使用 InputStream,OutputStream,Reader,Writer 的构造器
- [java.io.InputStream](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html),[OutputStream](http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html),[Reader](http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html) 和 [Writer](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html) 的所有子类都有一个使用 InputStream,OutputStream,Reader,Writer 的构造器
- [java.util.Collections](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html) 中的 [checkedXXX()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#checkedCollection%28java.util.Collection,%20java.lang.Class%29), [synchronizedXXX()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedCollection%28java.util.Collection%29) 和 [unmodifiableXXX()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#unmodifiableCollection%28java.util.Collection%29) 方法
- [javax.servlet.http.HttpServletRequestWrapper](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequestWrapper.html) 和 [HttpServletResponseWrapper](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponseWrapper.html)

Expand Down Expand Up @@ -148,4 +148,4 @@


- stackoverflow原址:
http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries
http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries
Loading

AltStyle によって変換されたページ (->オリジナル) /