JAVA构建树状结构
生活随笔
收集整理的這篇文章主要介紹了
JAVA构建树状结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
記一下自己在展示樹狀結構時候的幾個寫法,一個是確定有幾個層級的,用于只有兩三個層級的樹狀結構,寫法簡單一點。還有就是不確定有幾個層級,也可能1個,也可能4個或者更多。
先說一下確定有幾個層級的寫法
這是需要用的樹狀結構實體類
//父級菜單idprivate Long id;//父級菜單名稱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()); }明確幾個層級的寫法(這里是只有兩級),做了兩個foreach
//入參的list是查詢出來的父級菜單的信息 public List<TreeSelect> treeSelectList(List<XXX> indexInfoList) {List<TreeSelect> treeSelectList = new ArrayList<>();indexInfoList.stream().forEach(e -> {TreeSelect treeSelect = new TreeSelect();//設置大類信息if (e.getIndexPid() == 0) {treeSelect.setId(e.getId());treeSelect.setLabel(e.getIndexPname());List<TreeSelect> secondTree = new ArrayList<>();//根據id查詢小類信息QueryWrapper<XXX> queryWrapper = new QueryWrapper<IndexInfo>().eq("id", e.getId());List<XXX> indexInfos = indexInfoMapper.selectList(queryWrapper);//設置小類信息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;}下面是不確定幾個層級,遞歸寫法
方法類:
/*** 構建樹下拉菜單列表* @param indexInfoList* @return*/public List<TreeSelect> treeSelectList(List<IndexInfo> indexInfoList) {List<IndexInfo> trees = buildIndexTree(indexInfoList);return trees.stream().map(TreeSelect::new).collect(Collectors.toList());}/*** 構建樹結構* @param indexInfoList* @return*/public List<IndexInfo> buildIndexTree(List<IndexInfo> indexInfoList) {List<IndexInfo> returnList = new ArrayList<IndexInfo>();List<Long> indexList = new ArrayList<Long>();//一級菜單設置信息for (IndexInfo indexInfo : indexInfoList) {indexList.add(indexInfo.getId());}for (Iterator<IndexInfo> iterator = indexInfoList.iterator(); iterator.hasNext(); ) {IndexInfo indexInfo =iterator.next();// 如果是頂級節點, 遍歷該父節點的所有子節點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) {// 得到子節點列表List<IndexInfo> childList = getChildList(list, t);t.setChildren(childList);for (IndexInfo tChild : childList) {if (hasChild(list, tChild)) {recursionFn(list, tChild);}}}/*** 得到子節點列表*/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;}/*** 判斷是否有子節點*/private boolean hasChild(List<IndexInfo> list, IndexInfo t) {return getChildList(list, t).size() > 0 ? true : false;}附上結果:
"msg": "操作成功","code": 200,"data": [{"id": 1,"label": "指標名稱1""children": [{"id": 4,"label": "指標名稱1-1""children": [{"id": 10,"label": "指標名稱1-1-1"},{"id": 11,"label": "指標名稱1-1-2"}]},{"id": 5,"label": "指標名稱1-2"}]}] }總結
以上是生活随笔為你收集整理的JAVA构建树状结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Axure动态面板实现轮播图
- 下一篇: 【配准】空间变换网络Spatial Tr