Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Created October 17, 2018 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codethereforam/1df33d370c003d216c8207bc5184959f to your computer and use it in GitHub Desktop.
Save codethereforam/1df33d370c003d216c8207bc5184959f to your computer and use it in GitHub Desktop.
Multimap常用例子
// 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