Created
October 17, 2018 08:08
-
-
Save codethereforam/1df33d370c003d216c8207bc5184959f to your computer and use it in GitHub Desktop.
Multimap常用例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// brief (brief = complex) | |
public List<EnvConfigGroupVo> listEnvConfigGroupVoFrom1(List<EnvConfigVo> envConfigVoList) { | |
ListMultimap<Integer, EnvConfigVo> multimap = ArrayListMultimap.create(); | |
envConfigVoList.forEach(e -> multimap.put(e.getGroupType(), e)); | |
return multimap.keySet().stream() | |
.map(k -> new EnvConfigGroupVo(k, EnvConfigGroupTypeEnum.getByValue(k).getDesc(), multimap.get(k))) | |
.collect(Collectors.toList()); | |
} | |
// complex | |
public List<EnvConfigGroupVo> listEnvConfigGroupVoFrom2(List<EnvConfigVo> envConfigVoList) { | |
// 构造map: {'groupType': 'List<EnvConfigVo>'} | |
Map<Integer, List<EnvConfigVo>> map = new HashMap<>(16); | |
for (EnvConfigVo envConfigVo : envConfigVoList) { | |
Integer groupType = envConfigVo.getGroupType(); | |
List<EnvConfigVo> list = map.get(groupType); | |
if (list == null) { | |
list = new ArrayList<>(); | |
} | |
list.add(envConfigVo); | |
map.put(groupType, list); | |
} | |
List<EnvConfigGroupVo> envConfigGroupVoList = new ArrayList<>(); | |
for (Map.Entry<Integer, List<EnvConfigVo>> entry : map.entrySet()) { | |
EnvConfigGroupVo fieldSettingGroupDto = | |
new EnvConfigGroupVo(entry.getKey(), EnvConfigGroupTypeEnum.getByValue(entry.getKey()) | |
.getDesc(), entry.getValue()); | |
envConfigGroupVoList.add(fieldSettingGroupDto); | |
} | |
return envConfigGroupVoList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment