Skip to content

Commit 69d8e70

Browse files
使用枚举替代大量的if else
1 parent e3e4d20 commit 69d8e70

File tree

8 files changed

+106
-100
lines changed

8 files changed

+106
-100
lines changed

src/main/java/com/enumDemo/Color.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/com/enumDemo/ColorNumber.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/main/java/com/enumDemo/EnumDemo.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main/java/com/enumDemo/StatusDemo.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/java/com/enumDemo/StatusEnum.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.enumdemo;
2+
3+
public enum StatusEnum {
4+
Success("1", "成功"),
5+
Failure("2", "失败"),
6+
Wait("3", "等待"),
7+
8+
;
9+
10+
private String status;
11+
private String type;
12+
13+
StatusEnum(String type, String status) {
14+
this.status = status;
15+
this.type = type;
16+
}
17+
18+
public String getStatus() {
19+
return status;
20+
}
21+
22+
public void setStatus(String status) {
23+
this.status = status;
24+
}
25+
26+
public String getType() {
27+
return type;
28+
}
29+
30+
public void setType(String type) {
31+
this.type = type;
32+
}
33+
34+
/**
35+
* 遍历枚举常量,代替大量的if,else代码。
36+
*
37+
* @param type
38+
* @return
39+
*/
40+
public static String getStatusByValues(String type) {
41+
for (StatusEnum statusEnum : StatusEnum.values()) {
42+
if (statusEnum.getType().equals(type)) {
43+
return statusEnum.getStatus();
44+
}
45+
}
46+
return "";
47+
}
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.enumdemo;
2+
3+
4+
public class StatusEnumDemo {
5+
public static void main(String[] args) {
6+
String type="1";
7+
String status= StatusEnum.getStatusByValues(type);
8+
System.out.println(String.format("类型%s对应的状态为%s",type,status));
9+
}
10+
11+
/**
12+
* 不建议使用这样的写法。
13+
* 这种存在大量if,else的代码,可以使用枚举去处理。
14+
*
15+
* @param type
16+
* @return
17+
*/
18+
public static String getStatus(String type) {
19+
String status;
20+
if ("1".equals(type)) {
21+
status="成功";
22+
} else if ("2".equals(type)) {
23+
status = "失败";
24+
} else if ("3".equals(type)) {
25+
status = "等待";
26+
} else {
27+
status="";
28+
}
29+
return status;
30+
}
31+
32+
/**
33+
* 使用枚举常量
34+
*
35+
* @param type
36+
* @return
37+
*/
38+
public static String getStatusByType(String type) {
39+
String status=null;
40+
if (StatusEnum.Success.getType().equals(type)) {
41+
status=StatusEnum.Success.getStatus();
42+
}
43+
return status;
44+
}
45+
46+
47+
}

src/main/java/com/lambda/StreamMap.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55

66
public class StreamMap {
77
public static void main(String[] args) {
8+
// mapFuction();
9+
10+
mapToIntDemo();
11+
}
12+
13+
public static void mapFuction() {
814
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7);
915
list.stream().map( (x) -> x*x ).forEach(System.out::println);
1016
}
17+
18+
public static void mapToIntDemo() {
19+
List<String> list = Arrays.asList("1","2","3","4","5","6");
20+
list.stream().mapToInt(Integer::valueOf).forEach(System.out::println);
21+
}
1122
}

0 commit comments

Comments
 (0)