Skip to content

Commit b362115

Browse files
committed
优化“初始化静态map”
1 parent 35a7e63 commit b362115

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ stackoverflow-Java-top-qa
4141
* [去掉烦人的“!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
4242
* [获取完整的堆栈信息](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md)
4343
* [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md)
44+
* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
45+
4446

4547
> 网络
4648
@@ -92,7 +94,6 @@ stackoverflow-Java-top-qa
9294
- [How can I permanently have line numbers in IntelliJ?](http://stackoverflow.com/questions/13751/how-can-i-permanently-have-line-numbers-in-intellij)
9395
- [How do servlets work? Instantiation, shared variables and multithreading](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading)
9496
- [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)
95-
- [How can I Initialize a static Map?](http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map)
9697
- [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)
9798
- [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash)
9899
- [Uncatchable ChuckNorrisException](http://stackoverflow.com/questions/13883166/uncatchable-chucknorrisexception)

contents/How-can-I-Initialize-a-static-Map.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
怎么在Java中初始化一个静态的map
66

7-
**方法一**:静态初始化器
7+
我想到的两种方法如下,大家是否有更好的建议呢?
8+
9+
**方法一**:static初始化器
810

911
**方法二**:实例初始化(匿名子类)
1012

@@ -31,9 +33,9 @@
3133

3234
### 答案1 ###
3335

34-
匿名子类初始化器是java的语法糖,我搞不明白为什么匿名类来初始化,而且如果create的类是final的话,它将不起作用
36+
匿名子类初始化器是java的语法糖,我搞不明白为什么要用匿名子类来初始化,而且,如果类是final的话,它将不起作用
3537

36-
我创建固定大小图的时候使用static初始化器
38+
我使用static初始化器来创建一个固定长度的静态map
3739

3840
public class Test{
3941
private static final Map<Integer, String> myMap;
@@ -48,14 +50,14 @@
4850

4951
### 答案2 ###
5052

51-
我喜欢用Guava(是 Collection 框架的增强和扩张)的方法初始化一个静态的,不可改变的map
53+
我喜欢用Guava(是 Collection 框架的增强)的方法初始化一个静态的,不可改变的map
5254

5355
static fianl Map<Integer, String> myMap = ImmutablMap.of(
5456
1,"one",
5557
2, "two"
5658
)
5759
·
58-
当你的图entry的个数超过5个时候你就不能使用`ImmutableMap.of`可以试试`ImmutableMap.bulider()`
60+
当map的 entry个数超过5个时,你就不能使用`ImmutableMap.of`可以试试`ImmutableMap.bulider()`
5961

6062
static fianl Map<Integer, String> myMap = ImmutableMap.<Integer, String>builder()
6163
{

0 commit comments

Comments
 (0)