一、
一开始有段代码想看下怎么优化,原来是这样的:
public Map<String, Object> test() {
Map<String, Object> resMap = new LinkedHashMap<>();
List list = new ArrayList<>();
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
....
for(int i = 0, size = list.size(); i < size; i ++) {
Object obj = list.get(i);
switch(obj.getType()) {
case "1" : a1 += obj.getValue(); break;
case "2" : a2 += obj.getValue(); break;
case "3" : a3 += obj.getValue(); break;
case "4" : a4 += obj.getValue(); break;
case "5" : a5 += obj.getValue(); break;
....
}
}
resMap.put("a1", a1);
resMap.put("a2", a2);
resMap.put("a3", a3);
resMap.put("a4", a4);
resMap.put("a5", a5);
....
return resMap;
}
因为觉得字段达到 10 个以上,而且多处有。这样写以后维护都得滑界面,特别不爽,所以和同事闲聊得出一个用法,就是用 JDK8 的 stream,于是就改为了这样的代码:
public Map<String, Object> test() {
Map<String, Object> resMap = new LinkedHashMap<>();
List<MyObj> list = new ArrayList<MyObj>();
Map<String, Integer> sumMap = list.stream().collect(
Collectors.groupingBy(MyObj::getType, Collectors.summingInt(MyObj::getValue)
));
for(Map.Entry<String, Integer> entry : sumMap.entrySet()) {
switch (entry.getKey()) {
case "1" : resMap.put("a1", a1); break;
case "2" : resMap.put("a2", a2); break;
case "3" : resMap.put("a3", a3); break;
case "4" : resMap.put("a4", a4); break;
case "5" : resMap.put("a5", a5); break;
...
}
}
return resMap;
}
二、
虽然是短了,但是有个问题是:如果 list 集合没有了 a2 的元素,那么 resMap 岂不是没有 a2 元素 put 进去了?(原来定义了 a2 是必定会被 put 进去),如果另外再加一个提前 put 又会变的很长,不好管理:
public Map<String, Object> test() {
Map<String, Object> resMap = new LinkedHashMap<>();
// 提前放入预设
resMap.put("a1", 0);
resMap.put("a2", 0);
resMap.put("a3", 0);
resMap.put("a4", 0);
resMap.put("a5", 0);
....
List<MyObj> list = new ArrayList<MyObj>();
Map<String, Integer> sumMap = list.stream().collect(
Collectors.groupingBy(MyObj::getType, Collectors.summingInt(MyObj::getValue)
));
// 再补充
for(Map.Entry<String, Integer> entry : sumMap.entrySet()) {
switch (entry.getKey()) {
case "1" : resMap.put("a1", a1); break;
case "2" : resMap.put("a2", a2); break;
case "3" : resMap.put("a3", a3); break;
case "4" : resMap.put("a4", a4); break;
case "5" : resMap.put("a5", a5); break;
...
}
}
return resMap;
}
三、
为了管理,所以最后增加一个枚举类,并且将枚举放入 map 处理,并通过 code 进行获取具体的枚举信息:
public Enum MyEnum {
a1("1"),
a2("2"),
a3("3"),
a4("4"),
a5("5"),
...
;
private final static Map<String, MyEnum> cacheMap = new LinkedHashMap<>();
static {
for(MyEnum myEnum : MyEnum.values()) {
cacheMap.put(myEnum.getCode(), myEnum);
}
}
MyEnum(String code) {
this.code = code;
}
public static MyEnum getEnum(String code) {
return cacheMap.get(code);
}
public static boolean contains(String code) {
return cacheMap.containsKey(code);
}
private String code;
// getter
}
最后原来的代码就能改为这样:
public Map<String, Object> test() {
Map<String, Object> resMap = new LinkedHashMap<>();
List<MyObj> list = new ArrayList<MyObj>();
// 提前放入预设
for(MyEnum myEnum : MyEnum.values()) {
resMap.put(myEnum.name(), 0);
}
Map<String, Integer> sumMap = list.stream().collect(
Collectors.groupingBy(MyObj::getType, Collectors.summingInt(MyObj::getValue)
));
// 再补充
for(Map.Entry<String, Integer> entry : sumMap.entrySet()) {
if(MyEnum.contains(entry.getKey())) {
resMap.put(myEnum.getEnum(entry.getKey()).name(), entry.getValue());
}
}
return resMap;
}
确实短了不少,而且步骤也比较清晰,可以更好观看了。
(虽然是多了不少枚举,如果多出复用的话能省不少代码,而且后续还能继续抽象优化)
|