树形数据的缩进打印
- 缩进两个空格输出结果
效果图:
- 代码如下
import org.springframework.util.CollectionUtils;
import java.util.*;
public class Test{
static class Node {
int id;
int parentId;
String name;
public Node(int id, int parentId, String name) {
this.id = id;
this.parentId = parentId;
this.name = name;
}
}
public static void main(String[] args) {
List<Node> nodeList = Arrays.asList(
new Node(1, 0, "AA"),
new Node(2, 1, "BB"),
new Node(3, 1, "CC"),
new Node(4, 3, "DD"),
new Node(5, 3, "EE"),
new Node(6, 2, "FF"),
new Node(7, 2, "GG"),
new Node(8, 4, "HH"),
new Node(9, 5, "II"),
new Node(10, 0, "JJ"),
new Node(11, 10, "KK"),
new Node(12, 10, "LL"),
new Node(13, 12, "MM"),
new Node(14, 13, "NN"),
new Node(15, 14, "OO")
);
print(nodeList);
}
public static void print(List<Node> nodeList) {
Map<Integer,Node> map = new HashMap<>();
Map<Integer,List<Node>> reMap = new HashMap<>();
for ( int i = 0; i < nodeList.size(); i++){
Node node = nodeList.get(i);
map.put(node.id,nodeList.get(i));
}
for ( int i = 0; i < nodeList.size(); i++){
Node node = nodeList.get(i);
if(reMap.get(node.id) == null){
reMap.put(node.id,new ArrayList<>());
if (reMap.get(node.parentId) != null){
reMap.get(node.parentId).add(node);
}
}
}
for (int i = 0; i < nodeList.size(); i++) {
Node node = nodeList.get(i);
if(map.get(node.parentId)==null){
System.out.println(node.name);
List<Node> nodes = reMap.get(node.id);
soutResult(nodes," ",reMap);
}
}
}
private static void soutResult(List<Node> nodes,String str,Map<Integer,List<Node>> reMap) {
for (int i = 0; i < nodes.size(); i++) {
System.out.println(str+nodes.get(i).name);
List<Node> nodelist = reMap.get(nodes.get(i).id);
if (!CollectionUtils.isEmpty(nodelist)){
soutResult(nodelist,str+" ",reMap);
}
}
}
}
树形数据的封装
package com.sz.utils.file.entry;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class ModelType {
public Integer id;
public String name;
public Integer pid;
}
package com.sz.utils.file.entry;
import lombok.Data;
import java.util.List;
@Data
public class ModelTypeDTO {
public Integer id;
public String name;
public Integer pid;
List<ModelTypeDTO> children;
}
import com.sz.utils.file.entry.ModelType;
import com.sz.utils.file.entry.ModelTypeDTO;
import java.util.*;
public class DataOfTree {
public static void main(String[] args) {
List<ModelType> list = new ArrayList<>();
list.add(new ModelType(3, "机械键盘", 2));
list.add(new ModelType(4, "薄膜键盘", 2));
list.add(new ModelType(5, "主题键盘", 2));
list.add(new ModelType(6, "鼠标", 1));
list.add(new ModelType(7, "有线鼠标", 6));
list.add(new ModelType(8, "无线鼠标", 6));
list.add(new ModelType(10, "小米", 9));
list.add(new ModelType(11, "华为", 9));
list.add(new ModelType(12, "红米", 10));
list.add(new ModelType(13, "荣耀", 11));
list.add(new ModelType(14, "畅想", 11));
list.add(new ModelType(1, "数码", null));
list.add(new ModelType(2, "键盘", 1));
list.add(new ModelType(9, "手机", null));
List<ModelTypeDTO> modelTypeVos = toTree(list);
System.out.println(modelTypeVos);
}
static List<ModelTypeDTO> toTree(List<ModelType> list) {
HashMap<Integer, ModelTypeDTO> map = new HashMap<Integer, ModelTypeDTO>();
List<ModelTypeDTO> trees = new ArrayList<ModelTypeDTO>();
for (ModelType type : list) {
ModelTypeDTO node = new ModelTypeDTO();
node.setId(type.getId());
node.setName(type.getName());
node.setPid(type.getPid());
map.put(type.getId(), node);
}
for (ModelType type : list) {
Integer pid = type.getPid();
Integer id = type.getId();
ModelTypeDTO pNode = map.get(pid);
ModelTypeDTO cNode = map.get(id);
if (map.containsKey(pid)) {
List<ModelTypeDTO> children = pNode.getChildren();
if (children == null) {
children = new ArrayList<ModelTypeDTO>();
pNode.setChildren(children);
}
children.add(cNode);
} else {
trees.add(cNode);
}
}
return trees;
}
}