Skip to content

Commit ba475c8

Browse files
authored
Merge pull request giantray#94 from freezhan/Lookup-enum-by-string-value
giantray#93
2 parents 01b6166 + 9cc6dd8 commit ba475c8

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -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)

contents/lookup-enum-by-string-value.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
Java 中如何将 String 转换为 enum
2-
=======
1+
# Java 中如何将 String 转换为 enum
32

4-
###问题
5-
6-
###我有一个 enum 类
7-
8-
``` java
3+
### 问题
4+
enum 类
5+
```java
96
public enum Blah {
107
A, B, C, D
118
}
129
```
13-
我想要找到一个 `String` 对应的 enum 值。例如, `"A"` 将是 `Blah.A`.如何做到?
14-
15-
我需要使用 `Enum.valueOf()` 方法吗? 如果是该如何使用?
10+
如何根据枚举类型的值(比如 "A" ) 得到 `Blah.A`?
1611

1712
---
1813

1914
### A1
2015

2116
是的, `Blah.valueOf("A")` 将会给你 `Blah.A`.
2217

23-
静态方法 `valueof()``values()` 在编译时期被插入,并不存在于源码中。但是在Javadoc中;例如,[`Dialog.ModalityType`](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html "Dialog.ModalityType")中显示了这两个方法。
18+
静态方法 `valueof()``values()` 在编译时期被插入,并不存在于源码中。
19+
但是在Javadoc中会显示;例如,[`Dialog.ModalityType`](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html "Dialog.ModalityType")中显示了这两个方法。
2420

2521

2622
### A2
@@ -80,7 +76,7 @@ public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string)
8076
return null;
8177
}
8278
```
83-
之后,在我的enum类中通常如此使用来减少打字:
79+
之后,在我的enum类中通常如此使用来减少代码量:
8480
``` java
8581
public static MyEnum fromString(String name) {
8682
return getEnumFromString(MyEnum.class, name);
@@ -91,17 +87,20 @@ public static MyEnum fromString(String name) {
9187

9288
_评论区对于答主的异常处理一片指责 -译者注_
9389

94-
###A4
95-
如果你不想编写自己的工具类,可以使用 Google的 `guava` 库:
90+
### A4
91+
如果你不想编写自己的工具类,可以使用 Google的 [Google guava](https://github.com/google/guava) 库:
9692
``` java
9793
Enums.getIfPresent(Blah.class, "A")
9894
```
9995
它让你检查是否 `Blan`中存在 `A`并且不抛出异常
10096

10197
_完整方法签名 `Optional<T> getIfPresent(Class<T> enumClass, String value)` , `Optional` 对象可以优雅的解决null值问题 -译者注_
10298

99+
> 注意: 返回的是 `Google Optional` 而不是 `Java Optional`
100+
103101
---
104102
_其他的答案都大同小异,感兴趣的可以看原帖_
105103
stackoverflow链接
106-
http://stackoverflow.com/questions/604424/lookup-enum-by-string-value
107-
_译者:[MagicWolf](https://github.com/DaiDongLiang)_
104+
[Lookup enum by string value
105+
](https://stackoverflow.com/questions/604424/lookup-enum-by-string-value)
106+
_译者:[MagicWolf](https://github.com/DaiDongLiang)_

0 commit comments

Comments
 (0)