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

Commit 9e3a9cc

Browse files
Merge pull request #1 from giantray/master
update
2 parents ca5550b + db68803 commit 9e3a9cc

28 files changed

+1138
-183
lines changed

‎README.md‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ stackoverflow-Java-top-qa
99
对于参与翻译的人,这也是很好的一个学习、理解过程,欢迎大家一起来翻译
1010

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

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

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

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

3030

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

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

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

111123
> 测试
112124
@@ -118,34 +130,21 @@ stackoverflow-Java-top-qa
118130
* [在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)
119131
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
120132
* [安装Android SDK的时候找不到JDK](contents/android-sdk-installation-doesnt-find-jdk.md)
133+
* [安卓中"UserManger.isUserAGoat()"的合适案例](contents/proper-use-cases-for-android-usermanager-isuseragoat.md)
121134

122-
### 待翻译问题链接(还剩x问题)
123-
-[Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
135+
136+
### 待翻译问题链接(还剩 13 问题)
124137
- [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)
125-
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
126-
- ['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)
127138
- [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)
128139
- [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)
129-
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
130-
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
131-
- [What exactly is Apache Camel?](http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel)
132140
- [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)
133141
- [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)
134142
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)
135143
- [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)
136144
- [Update Eclipse with Android development tools v. 23](http://stackoverflow.com/questions/24437564/update-eclipse-with-android-development-tools-v-23)
137-
- [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath)
138145
- [What is the Java equivalent for LINQ?](http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq)
139146
- [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do)
140-
- [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)
141-
- [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)
142-
- [Efficiency of Java "Double Brace Initialization"?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)
143-
- [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop)
144147
- [decompiling DEX into Java sourcecode](http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode)
145-
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
146-
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
147-
- [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)
148-
- [How do I "decompile" Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
149148
- [Useful Eclipse Java Code Templates [closed]](http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates)
150149
- [How to call SOAP web service in Android](http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-in-android)
151150

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
为自动代码调整设置最大的行数?
2+
问题:我正在学习Java。如果我在Eclipse Helios里使用ctrl+shift+f的组合键,它会自动调整我的代码。一定程度下,它会改变行数。我想增加行数的最大值。应该怎么做?
3+
4+
回答,在偏好设置里,分别点击Java->Code Style->Fomatter->edit,在菜单栏Line Wrapping下会有行的宽度选择(Maximun line width).你将需要编辑你的代码轮廓。
5+
6+
stackoverflow原址:http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting

‎contents/avoiding-null-statements-in-java.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ ParserFactory.getParser().findAction(someInput).doSomething();
9999
- 如果你想返回null,请停下来想一想,这个地方是否更应该抛出一个异常
100100

101101
stackoverflow链接:
102-
http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java?page=2&tab=votes#tab-top
102+
http://stackoverflow.com/questions/271526/avoiding-null-statements?page=1&tab=votes#tab-top

‎contents/convert-a-string-to-an-enum-in-java.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ Enum.valueOf()是否能实现以上目的,如果是,那我如何使用?
1919

2020
### 其他答案
2121

22+
当文本和枚举值不同时,可以采用这种方式:
23+
```java
24+
public enum Blah {
25+
A("text1"),
26+
B("text2"),
27+
C("text3"),
28+
D("text4");
29+
30+
private String text;
31+
32+
Blah(String text) {
33+
this.text = text;
34+
}
35+
36+
public String getText() {
37+
return this.text;
38+
}
39+
40+
public static Blah fromString(String text) {
41+
for (Blah b : Blah.values()) {
42+
if (b.text.equalsIgnoreCase(text)) {
43+
return b;
44+
}
45+
}
46+
return null;
47+
}
48+
}
49+
```
50+
fromString方法中,throw new IllegalArgumentException("No constant with text " + text + " found") 会比直接返回null更优秀.
51+
52+
### 其他答案
53+
2254
我有一个挺赞的工具方法:
2355
```java
2456
/**
@@ -47,4 +79,4 @@ public static MyEnum fromString(String name) {
4779
}
4880
```
4981

50-
stackoverflow链接:http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
82+
stackoverflow链接:http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
## "Double Brace Initialization"的效率问题
2+
3+
#### 问题
4+
5+
`Double Brace Initialization`是java的隐藏特性,它有着如下诱人的语法:
6+
7+
```java
8+
Set<String> flavors = new HashSet<String>() {{
9+
add("vanilla");
10+
add("strawberry");
11+
add("chocolate");
12+
add("butter pecan");
13+
}};
14+
```
15+
16+
但是,这个特性听说不是很高效率,是否要限制一次性使用?
17+
18+
#### 回答
19+
20+
当我使用匿名内部类时出现了如下的问题:
21+
22+
```Auto
23+
2009年05月27日 16:35 1,602 DemoApp21ドル.class
24+
2009年05月27日 16:35 1,976 DemoApp210ドル.class
25+
2009年05月27日 16:35 1,919 DemoApp211ドル.class
26+
2009年05月27日 16:35 2,404 DemoApp212ドル.class
27+
2009年05月27日 16:35 1,197 DemoApp213ドル.class
28+
29+
/* snip */
30+
31+
2009年05月27日 16:35 1,953 DemoApp230ドル.class
32+
2009年05月27日 16:35 1,910 DemoApp231ドル.class
33+
2009年05月27日 16:35 2,007 DemoApp232ドル.class
34+
2009年05月27日 16:35 926 DemoApp233ドル1ドル1ドル.class
35+
2009年05月27日 16:35 4,104 DemoApp233ドル1ドル.class
36+
2009年05月27日 16:35 2,849 DemoApp233ドル.class
37+
2009年05月27日 16:35 926 DemoApp234ドル1ドル1ドル.class
38+
2009年05月27日 16:35 4,234 DemoApp234ドル1ドル.class
39+
2009年05月27日 16:35 2,849 DemoApp234ドル.class
40+
41+
/* snip */
42+
43+
2009年05月27日 16:35 614 DemoApp240ドル.class
44+
2009年05月27日 16:35 2,344 DemoApp25ドル.class
45+
2009年05月27日 16:35 1,551 DemoApp26ドル.class
46+
2009年05月27日 16:35 1,604 DemoApp27ドル.class
47+
2009年05月27日 16:35 1,809 DemoApp28ドル.class
48+
2009年05月27日 16:35 2,022 DemoApp29ドル.class
49+
```
50+
51+
这是在我的一个简单应用中所产生的类信息。在这个应用中,使用了大量的匿名内部类,这些类会被单独地编译成`class`文件。
52+
53+
`Double Brace Initialization`是一个带有实例初始化块的匿名内部类。这就意味着每一个新的类的产生都会执行一次实例块,这样的目的通常是为了创建一个简单的对象。
54+
55+
java虚拟机在使用类之前需要去读取其classes信息,然后执行字节码校验等流程。所以为了保存这些`class`文件,所需要的磁盘空间会增大。
56+
57+
这个可以说是`Double Brace Initialization`的开销。所以尽量不要过分使用。
58+
59+
---
60+
61+
在java的介绍中,`Double Brace Initialization`有着如下的写法:
62+
63+
```java
64+
List<String> list = new ArrayList<String>() {{
65+
add("Hello");
66+
add("World!");
67+
}};
68+
```
69+
70+
看起来像是java的隐藏特性,其实它只是下面代码的一个重写:
71+
72+
```java
73+
List<String> list = new ArrayList<String>() {
74+
75+
// 实例初始化块
76+
{
77+
add("Hello");
78+
add("World!");
79+
}
80+
};
81+
```
82+
83+
所以,它只是在匿名内部类中加上了实例初始化块而已。
84+
85+
---
86+
87+
Joshua Bloch希望以后的集合代码能够像这样简介:
88+
89+
```java
90+
List<Integer> intList = [1, 2, 3, 4];
91+
92+
Set<String> strSet = {"Apple", "Banana", "Cactus"};
93+
94+
Map<String, Integer> truthMap = { "answer" : 42 };
95+
```
96+
97+
但目前还没有这样的语法。
98+
99+
---
100+
101+
<strong>实践</strong>
102+
103+
做一个简单的试验:创建1000个带着`"Hello"``"World!"`元素的`ArrayList`
104+
105+
* 方法1:Double Brace Initialization
106+
107+
```
108+
List<String> l = new ArrayList<String>() {{
109+
add("Hello");
110+
add("World!");
111+
}};
112+
```
113+
114+
* 方法2:初始化`ArrayList`并调用`add`方法
115+
116+
```java
117+
List<String> l = new ArrayList<String>();
118+
l.add("Hello");
119+
l.add("World!");
120+
```
121+
122+
我修改了java的源文件使之能够为每种上述方法分别创建出1000个实例
123+
124+
* 方法1的测试
125+
126+
```java
127+
class Test1 {
128+
public static void main(String[] s) {
129+
long st = System.currentTimeMillis();
130+
131+
List<String> l0 = new ArrayList<String>() {{
132+
add("Hello");
133+
add("World!");
134+
}};
135+
136+
List<String> l1 = new ArrayList<String>() {{
137+
add("Hello");
138+
add("World!");
139+
}};
140+
141+
/* snip */
142+
143+
List<String> l999 = new ArrayList<String>() {{
144+
add("Hello");
145+
add("World!");
146+
}};
147+
148+
System.out.println(System.currentTimeMillis() - st);
149+
}
150+
```
151+
152+
* 方法2的测试
153+
154+
```java
155+
class Test2 {
156+
public static void main(String[] s) {
157+
long st = System.currentTimeMillis();
158+
159+
List<String> l0 = new ArrayList<String>();
160+
l0.add("Hello");
161+
l0.add("World!");
162+
163+
List<String> l1 = new ArrayList<String>();
164+
l1.add("Hello");
165+
l1.add("World!");
166+
167+
/* snip */
168+
169+
List<String> l999 = new ArrayList<String>();
170+
l999.add("Hello");
171+
l999.add("World!");
172+
173+
System.out.println(System.currentTimeMillis() - st);
174+
}
175+
}
176+
```
177+
178+
然后得出了下面的测试时间:
179+
180+
```Auto
181+
Test1 Times (ms) Test2 Times (ms)
182+
---------------- ----------------
183+
187 0
184+
203 0
185+
203 0
186+
188 0
187+
188 0
188+
187 0
189+
203 0
190+
188 0
191+
188 0
192+
203 0
193+
```
194+
195+
从上面我们可以看到,`Double Brace Initialization`平均时间花费了190ms左右。
196+
同时,另外一种方法平均只用了0ms。
197+
198+
所以,在第一个方法测试程序中,即`Double Brace Initialization`产生了1000个`class`文件。
199+
200+
## StackOverflow地址
201+
202+
[http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization)

0 commit comments

Comments
(0)

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