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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

JAVA构建树状结构

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

記一下自己在展示樹(shù)狀結(jié)構(gòu)時(shí)候的幾個(gè)寫法,一個(gè)是確定有幾個(gè)層級(jí)的,用于只有兩三個(gè)層級(jí)的樹(shù)狀結(jié)構(gòu),寫法簡(jiǎn)單一點(diǎn)。還有就是不確定有幾個(gè)層級(jí),也可能1個(gè),也可能4個(gè)或者更多。

先說(shuō)一下確定有幾個(gè)層級(jí)的寫法

這是需要用的樹(shù)狀結(jié)構(gòu)實(shí)體類

//父級(jí)菜單idprivate Long id;//父級(jí)菜單名稱private String label;//子菜單信息private List<TreeSelect> children;public TreeSelect() {}public TreeSelect(XXX xxx) {this.id = xxx.getId();this.label = xxx.gexxname();this.professorName = xxx.getxxxame();this.children = xxx.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList()); }

明確幾個(gè)層級(jí)的寫法(這里是只有兩級(jí)),做了兩個(gè)foreach

//入?yún)⒌膌ist是查詢出來(lái)的父級(jí)菜單的信息 public List<TreeSelect> treeSelectList(List<XXX> indexInfoList) {List<TreeSelect> treeSelectList = new ArrayList<>();indexInfoList.stream().forEach(e -> {TreeSelect treeSelect = new TreeSelect();//設(shè)置大類信息if (e.getIndexPid() == 0) {treeSelect.setId(e.getId());treeSelect.setLabel(e.getIndexPname());List<TreeSelect> secondTree = new ArrayList<>();//根據(jù)id查詢小類信息QueryWrapper<XXX> queryWrapper = new QueryWrapper<IndexInfo>().eq("id", e.getId());List<XXX> indexInfos = indexInfoMapper.selectList(queryWrapper);//設(shè)置小類信息indexInfos.stream().forEach(indexInfo -> {TreeSelect secondTreeSelect = new TreeSelect();secondTreeSelect.setId(indexInfo.getId());secondTreeSelect.setLabel(indexInfo.getIndexPname());secondTree.add(secondTreeSelect);});//添加集合treeSelect.setChildren(secondTree);treeSelectList.add(treeSelect);}});return treeSelectList;}

下面是不確定幾個(gè)層級(jí),遞歸寫法

方法類:

/*** 構(gòu)建樹(shù)下拉菜單列表* @param indexInfoList* @return*/public List<TreeSelect> treeSelectList(List<IndexInfo> indexInfoList) {List<IndexInfo> trees = buildIndexTree(indexInfoList);return trees.stream().map(TreeSelect::new).collect(Collectors.toList());}/*** 構(gòu)建樹(shù)結(jié)構(gòu)* @param indexInfoList* @return*/public List<IndexInfo> buildIndexTree(List<IndexInfo> indexInfoList) {List<IndexInfo> returnList = new ArrayList<IndexInfo>();List<Long> indexList = new ArrayList<Long>();//一級(jí)菜單設(shè)置信息for (IndexInfo indexInfo : indexInfoList) {indexList.add(indexInfo.getId());}for (Iterator<IndexInfo> iterator = indexInfoList.iterator(); iterator.hasNext(); ) {IndexInfo indexInfo =iterator.next();// 如果是頂級(jí)節(jié)點(diǎn), 遍歷該父節(jié)點(diǎn)的所有子節(jié)點(diǎn)if (!indexList.contains(indexInfo.getIndexPid())) {recursionFn(indexInfoList, indexInfo);returnList.add(indexInfo);}}if (returnList.isEmpty()) {returnList = indexInfoList;}return returnList;}/*** 遞歸列表*/private void recursionFn(List<IndexInfo> list, IndexInfo t) {// 得到子節(jié)點(diǎn)列表List<IndexInfo> childList = getChildList(list, t);t.setChildren(childList);for (IndexInfo tChild : childList) {if (hasChild(list, tChild)) {recursionFn(list, tChild);}}}/*** 得到子節(jié)點(diǎn)列表*/private List<IndexInfo> getChildList(List<IndexInfo> list, IndexInfo t) {List<IndexInfo> tlist = new ArrayList<IndexInfo>();Iterator<IndexInfo> it = list.iterator();while (it.hasNext()) {IndexInfo n = it.next();if (StringUtils.isNotNull(n.getIndexPid()) && n.getIndexPid().longValue() == t.getId().longValue()) {tlist.add(n);}}return tlist;}/*** 判斷是否有子節(jié)點(diǎn)*/private boolean hasChild(List<IndexInfo> list, IndexInfo t) {return getChildList(list, t).size() > 0 ? true : false;}

附上結(jié)果:

"msg": "操作成功","code": 200,"data": [{"id": 1,"label": "指標(biāo)名稱1""children": [{"id": 4,"label": "指標(biāo)名稱1-1""children": [{"id": 10,"label": "指標(biāo)名稱1-1-1"},{"id": 11,"label": "指標(biāo)名稱1-1-2"}]},{"id": 5,"label": "指標(biāo)名稱1-2"}]}] }

總結(jié)

以上是生活随笔為你收集整理的JAVA构建树状结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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