递归统计
實(shí)現(xiàn)多叉樹聚合統(tǒng)計(jì):每一個(gè)節(jié)點(diǎn)包含用戶信息,統(tǒng)計(jì)包含的用戶總數(shù)(節(jié)點(diǎn)的用戶總數(shù)是下面所有子節(jié)點(diǎn)的聚合統(tǒng)計(jì))
首先掛所有的數(shù)據(jù):
private void traverseChild (List<Department> parentList, List<Department> allChild, Map<Long, Department> mapNode) {for(int i = 0; i < parentList.size(); i++) {int currentOnline = 0;//添加子節(jié)點(diǎn)for(int j = 0; j < allChild.size(); j++) {if(parentList.get(i).getId() == allChild.get(j).getParentId()) {if(parentList.get(i).getChild() == null) {parentList.get(i).setChild(new ArrayList<Department>());}if(allChild.get(j).getUsers() != null && allChild.get(j).getUsers().size() >0) {int online = 0;for(int k = 0; k < allChild.get(j).getUsers().size(); k++) {if(allChild.get(j).getUsers().get(k).getOnline() != null && allChild.get(j).getUsers().get(k).getOnline() == 1) {online++;}}allChild.get(j).setOnlineUserCount(online);allChild.get(j).setTotal(allChild.get(j).getUsers().size());}parentList.get(i).getChild().add(allChild.get(j));}}if(parentList.get(i).getUsers() != null && parentList.get(i).getUsers().size() > 0) {parentList.get(i).setTotal(parentList.get(i).getUsers().size());for(int n = 0; n < parentList.get(i).getUsers().size(); n++) {if(parentList.get(i).getUsers().get(n).getOnline() != null && parentList.get(i).getUsers().get(n).getOnline() == 1) {currentOnline++;}}parentList.get(i).setOnlineUserCount(currentOnline);}//遞歸遍歷子節(jié)點(diǎn)if(parentList.get(i).getChild() != null && parentList.get(i).getChild().size() > 0) {mapNode.put(parentList.get(i).getId(), parentList.get(i));traverseChild(parentList.get(i).getChild(), allChild, mapNode);} else {//葉子parentList.get(i).setIsCount(true);mapNode.put(parentList.get(i).getId(), parentList.get(i));}}}把所有的節(jié)點(diǎn)重復(fù)掃描:
private List<Department> countNode (Map<Long, Department> map, List<Department> parent) {//重復(fù)掃描計(jì)算traverseMapCountNode(map);List<Department> root = new ArrayList<Department>();parent.forEach(p -> {root.add(map.get(p.getId()));});return root;}private void traverseMapCountNode(Map<Long, Department> mapNode) {int nodeNum = 0;for (Map.Entry<Long, Department> entry : mapNode.entrySet()) {if(entry.getValue().getIsCount()) {nodeNum++;continue;} else {//非葉子if(entry.getValue().getChild() != null || entry.getValue().getChild().size() > 0) {int len = 0;for(int i = 0; i < entry.getValue().getChild().size(); i++) {if(entry.getValue().getChild().get(i).getIsCount()) {len++;}}if(len == entry.getValue().getChild().size()) {for(int i = 0; i < entry.getValue().getChild().size(); i++) {entry.getValue().setTotal(entry.getValue().getTotal() + entry.getValue().getChild().get(i).getTotal()); entry.getValue().setOnlineUserCount(entry.getValue().getOnlineUserCount() + entry.getValue().getChild().get(i).getOnlineUserCount());} entry.getValue().setIsCount(true); } else {continue;}}}}if(nodeNum < mapNode.size()) {traverseMapCountNode(mapNode);} }?
?
總結(jié)
- 上一篇: 白嫖 GPT-4 最强竞品,20 秒速通
- 下一篇: A Dream