private List<AntdTree> getBanJiTree(List<Map<String, String>> maps, int level
, List<Map<String, String>> childList
, AntdTree treeNode) {
List<AntdTree> resultList = new LinkedList<>();
String idName = "yxid";
String mcName = "yxmc";
String type = "xy";
String nextName = "zymc";
switch (level) {
case 1:
idName = "yxid";
mcName = "yxmc";
type = "xy";
nextName = "zymc";
break;
case 2:
idName = "zyid";
mcName = "zymc";
type = "zy";
nextName = "njmc";
break;
case 3:
idName = "njid";
mcName = "njmc";
type = "nj";
nextName = "bjmc";
break;
case 4:
idName = "bjid";
mcName = "bjmc";
type = "bj";
nextName = "bjmc";
break;
}
final String groupIdName = idName;
final String sortName = nextName;
Map<String, List<Map<String, String>>> groupList = null;
//归组,保持原顺序
if (level == 1) {
groupList = maps.stream()
.collect(Collectors.groupingBy(f -> f.get(groupIdName), LinkedHashMap::new, Collectors.toList()));
} else {
groupList = childList.stream()
.collect(Collectors.groupingBy(f -> f.get(groupIdName), LinkedHashMap::new, Collectors.toList()));
}
for (Map.Entry<String, List<Map<String, String>>> entry :
groupList.entrySet()) {
AntdTree node = new AntdTree();
node.setKey(entry.getKey());
node.setTitle(maps.stream().filter(f -> f.get(groupIdName).equals(entry.getKey())).findFirst().get().get(mcName));
node.setType(type);
if (level < 4) {
node.setChildren(new LinkedList<>());
childList = entry.getValue();
//排序
List<Map<String, String>> collect = childList.stream()
.sorted(Comparator.comparing(item -> item.get(sortName)))
.collect(Collectors.toList());
getBanJiTree(maps, level + 1, collect, node);
}
if (level == 1) {
resultList.add(node);
} else {
treeNode.getChildren().add(node);
}
}
return resultList;
}
使用
List<Map<String, String>> maps = banJiService.listAll();
//排序
List<Map<String, String>> collect = maps.stream()
.sorted(Comparator.comparing(item -> item.get("yxmc")))
.collect(Collectors.toList());
List<AntdTree> banJiTree = getBanJiTree(collect, 1, null, null);
实现了一个将学院,专业,年级,班级组合起来的树,每个层级都按名称进行升序排列
归组统计:
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(EnquirySign::getPublishId, Collectors.counting()));