需要组装成树的返回实体需要实现该类
public interface TreeStructure<T, S extends TreeStructure> extends Comparable<S>{
T getParentId();
T getId();
Collection<S> getChildren();
void setChildren(Collection<S> children);
default Integer getSortIndex(){
return 0;
}
@Override
default int compareTo(S o) {
if(o == null){
return -1;
}
if(this.getSortIndex() == null && o.getSortIndex() == null){
if(getId() instanceof Long && o.getId() instanceof Long){
return ((Long) getId()).longValue() < ((Long) o.getId()).longValue() ? -1 : 1;
}
return 0;
}
if(this.getSortIndex() == null){
return -1;
}
if(o.getSortIndex() == null){
return 1;
}
if(this.getSortIndex().intValue() == o.getSortIndex().intValue()){
if(getId() instanceof Long && o.getId() instanceof Long){
return ((Long) getId()).longValue() < ((Long) o.getId()).longValue() ? -1 : 1;
}
return 0;
}
return this.getSortIndex().intValue() < o.getSortIndex().intValue() ? -1 : 1;
}
static <T, S extends TreeStructure<T, S>> List<S> buildTreeList(List<S> list, T rootId) {
if(CollectionUtils.isEmpty(list)){
return Collections.emptyList();
}
Map<T, S> map = list.stream().collect(Collectors.toMap(TreeStructure::getId, Function.identity(), (k1, k2) -> k1));
List<S> sortedList = list.stream().sorted().collect(Collectors.toList());
List<S> treeList = new LinkedList<>();
for(S t : sortedList){
T parentId = t.getParentId();
if(map.containsKey(parentId)){
S parent = map.get(parentId);
Collection<S> children = parent.getChildren();
if(children == null){
children = new LinkedList<>();
parent.setChildren(children);
}
children.add(t);
}
if(parentId != null && parentId.equals(rootId)){
treeList.add(t);
}
}
map.clear();
return treeList;
}
static <T, S extends TreeStructure<T, S>> List<S> buildTreeList(List<S> list) {
if(CollectionUtils.isEmpty(list)){
return Collections.emptyList();
}
Map<T, S> map = list.stream().collect(Collectors.toMap(TreeStructure::getId, Function.identity(), (k1, k2) -> k1));
List<S> sortedList = list.stream().sorted().collect(Collectors.toList());
sortedList.removeIf(t -> {
T parentId = t.getParentId();
if(map.containsKey(parentId)){
S parent = map.get(parentId);
Collection<S> children = parent.getChildren();
if(children == null){
children = new LinkedList<>();
parent.setChildren(children);
}
children.add(t);
return true;
}
return false;
});
map.clear();
return sortedList;
}
static <S extends TreeStructure> S buildRootTree(List<S> list, S root){
root.setChildren(buildTreeList(list, root.getId()));
return root;
}
static <S extends TreeStructure<T,S>, T> S buildTree(List<S> list, T rootId){
S root = list.stream().filter(item -> item.getId().equals(rootId)).findAny()
.orElseThrow(() -> BusinessException.create(SystemErrorCode.INTERNAL_SERVER_ERROR));
root.setChildren(buildTreeList(list, root.getId()));
return root;
}
static <S extends TreeStructure> void filterTreeIncludeChildren(Collection<S> treeCollection, Predicate<S> predicate){
if(CollectionUtils.isEmpty(treeCollection)){
return;
}
treeCollection.removeIf(r -> {
if(predicate.test(r)) return false;
Collection<S> children = r.getChildren();
filterTreeIncludeChildren(children, predicate);
if(CollectionUtils.isEmpty(children)) return true;
return false;
});
}
static <T extends TreeStructure> List<T> treeTransList(List<T> tree) {
List<T> result = new ArrayList<>();
Stack<T> executeCurStack = new Stack<>();
for (T item : tree) {
result.add(item);
if (Objects.isNull(item.getChildren()) || CollectionUtils.isEmpty(item.getChildren())) continue;
item.getChildren().forEach(child -> executeCurStack.push((T) child));
item.setChildren(null);
while (!executeCurStack.isEmpty()) {
if (!CollectionUtils.isEmpty(executeCurStack.peek().getChildren())) {
Collection children = executeCurStack.peek().getChildren();
executeCurStack.peek().setChildren(null);
children.forEach(child -> executeCurStack.push((T) child));
continue;
}
result.add(executeCurStack.pop());
}
}
return result;
}
static <S extends TreeStructure> void filterTree(Collection<S> treeCollection, Predicate<S> predicate){
if(CollectionUtils.isEmpty(treeCollection)) return;
treeCollection.removeIf(r -> {
Collection<S> children = r.getChildren();
filterTree(children, predicate);
if(!CollectionUtils.isEmpty(children)) return false;
return !predicate.test(r);
});
}
class SortedLinkedList<T extends TreeStructure> extends LinkedList<T> {
@Override
public boolean add(T t) {
if(size() == 0){
add(0, t);
return true;
} else {
T value = t;
int x;
for(x = size(); x > 0; x--){
if (value.compareTo(get(x - 1)) > 0){
break;
}
}
add(x, value);
return true;
}
}
}
}```
## 实体
```java
@Data
public class TreeViewModel implements Serializable, TreeStructure<Long, TreeViewModel> {
private Long id;
private Long parentId;
private String name;
private String shortName;
private Collection<TreeViewModel> children;
private Integer sortIndex;
}
调用方式
List<TreeViewModel> treeViewModelList = TreeStructure.buildTreeList(viewModelList, findViewTreeModel.getId());
展示效果图类似于这种
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/d772747b4e754a4eb5419bde3426791f.png)