日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

递归统计

發(fā)布時(shí)間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 递归统计 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

實(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é)

以上是生活随笔為你收集整理的递归统计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。