TODO树形结构Hash

This commit is contained in:
zhaokaiyao@91isoft.com
2021-10-20 14:49:51 +08:00
parent ad91f84dfb
commit 7b87743f9f
2 changed files with 94 additions and 6 deletions
@@ -11,6 +11,8 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -73,4 +75,12 @@ public class TsInstitution extends BaseEntity {
this.parentId = parentId;
this.name = name;
}
// public void addChildren(TsInstitution node){
// if (this.children==null){
// this.children=Arrays.asList(node);
// }else {
// this.children.add(node);
// }
// }
}
@@ -3,10 +3,7 @@ package com.adc.da.slrs.sarInstitution.util;
import com.adc.da.slrs.sarInstitution.entity.TsInstitution;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
@Component
@@ -41,8 +38,9 @@ public class TreeUtil {
* @return
*/
private List<TsInstitution> getChildNode(TsInstitution root, List<TsInstitution> contentKnowledgeList) {
List<TsInstitution> childrenList = contentKnowledgeList.stream().filter(e -> Objects.equals(e.getParentId(), root.getId())).map(
e -> {
List<TsInstitution> childrenList = contentKnowledgeList.stream()
.filter(e -> Objects.equals(e.getParentId(), root.getId()))
.map(e -> {
List<TsInstitution> childNode = getChildNode(e, contentKnowledgeList);
e.setChildren(new ArrayList<>(childNode));
return e;
@@ -51,4 +49,84 @@ public class TreeUtil {
return childrenList;
}
// public List<TsInstitution> getChildAsHash(TsInstitution root, Map<String, TsInstitution> hashInstitution){
// String id = root.getId();
// if (hashInstitution.containsKey(id)){
//
// }
// return null;
// }
// //TODO 利用HashMap组装树结构
//
// public List<TsInstitution> treeAsHash(List<TsInstitution> institutionList,List<TsInstitution> rootList) {
//
// Map<String, TsInstitution> idMap = institutionList.stream()
// .collect(Collectors.toMap(TsInstitution::getId, tsInstitution -> tsInstitution));
//
// Map<String, TsInstitution> pid_institution = institutionList.stream()
// .collect(Collectors.toMap(TsInstitution::getParentId, institution -> institution));
//
// for (TsInstitution institution : rootList) {
//
// String id = institution.getId();
// if (pid_institution.containsKey(id)) {
// institution.addChildren(pid_institution.get(id));
//
// }
//
// }
//
//
// Set<String> rootSet = rootList.stream()
// .map(TsInstitution::getId)
// .collect(Collectors.toSet());
//
// Iterator<TsInstitution> it = institutionList.iterator();
//
// ArrayList<TsInstitution> resultList = new ArrayList<>();
//
//
// HashMap<String, TsInstitution> id_obj = new HashMap<>();
// while (it.hasNext()) {
// TsInstitution next = it.next();
//
// String parentId = next.getParentId();
// id_obj.put(next.getId(), next);
// if (rootSet.contains(parentId)) {
// id_obj.put(next.getId(), next);
// } else if (id_obj.containsKey(parentId)) {
// id_obj.get(parentId).addChildren(next);
// }
//
// }
// return null ;
//
// }
//
//
//
// List<TreeNodeDTO> list = dbMapper.getNodeList();
// ArrayList<TreeNodeDTO> rootNodes = new ArrayList<>();
// Map<Integer, TreeNodeDTO> map = new HashMap<>();
// for (TreeNodeDTO node :list) {
// map.put(node.getId(), node);
// Integer parentId = node.getParentId();
// // 判断是否有父节点 (没有父节点本身就是个父菜单)
// if (parentId.equals('0')){
// rootNodes.add(node);
// // 找出不是父级菜单的且集合中包括其父菜单ID
// } else if (map.containsKey(parentId)){
// map.get(parentId).getChildren().add(node);
// }
// }
//
// }
}