import java.util.List;
public class TreeDto {
public TreeDto(String id, String pid, String name, String treeNo){
this.id=id;
this.pid = pid;
this.name = name;
this.treeNo = treeNo;
}
private String id;
private String pid;//fatherid
private String name;
private String treeNo;
private List<TreeDto> children;
//省略get和set
}
import dto.TreeDto;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class listToTreeTest {
public static void main(String[] args) {
TreeDto treeDto1 = new TreeDto("1","0","学生1","01");
TreeDto treeDto2 = new TreeDto("11","1","家长1","0101");
TreeDto treeDto3 = new TreeDto("12","1","家长2","0102");
TreeDto treeDto4 = new TreeDto("2","0","学生2","02");
TreeDto treeDto5 = new TreeDto("3","0","学生3","03");
TreeDto treeDto6 = new TreeDto("4","11","第3层","010101");
List<TreeDto> testTree = new ArrayList<>();
testTree.add(treeDto1);
testTree.add(treeDto2);
testTree.add(treeDto3);
testTree.add(treeDto4);
testTree.add(treeDto5);
testTree.add(treeDto6);
System.out.println(JSON.toJSON(listToTree(testTree)));
}
// 列表转树结构
private static List<TreeDto> listToTree(List<TreeDto> oldList){
Map<String,Object> newMap = new HashMap<>();
List<TreeDto> newList = new ArrayList<>();
for (TreeDto treeDto : oldList){
newMap.put(treeDto.getId(),treeDto);
}
for (TreeDto treeDto : oldList){
TreeDto parent = (TreeDto) newMap.get(treeDto.getPid());
if (parent != null){
if (parent.getChildren() == null){
List<TreeDto> ch = new ArrayList<>();
ch.add(treeDto);
parent.setChildren(ch);
}else {
List<TreeDto> ch = parent.getChildren();
ch.add(treeDto);
parent.setChildren(ch);
}
}else {
newList.add(treeDto);
}
}
return newList;
}
}
把方法里面需要用到的字段放到父类里面,需要转成树的类继承父类,就能改成通用的。考虑到如果需要转换的类都已经继承了某一公共类不能再继承了,改成抽象,抽象出id,父id和子集,重写就能用。
package myBaseTree;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class list2tree<N> {
/**
* 获取节点唯一标识方法
* @param node
* @return
*/
protected abstract String getKey(N node);
/**
* 获取节点父节点唯一标识方法
* @param node
* @return
*/
protected abstract String getParentId(N node);
/**
* 子节点
* @param node
* @return
*/
protected abstract List<N> getChildrens(N node);
protected abstract void setChildrens(List<N> nodes,N node);
/**
* 生成树方法
* @param oldList
* @return
*/
//@Override
public List<N> listToTree(List<N> oldList) {
Map<String,Object> newMap = new HashMap<>();
List<N> newList = new ArrayList<>();
for (N tree : oldList){
newMap.put(getKey(tree),tree);
}
for (N tree : oldList){
N parent = (N) newMap.get(getParentId(tree));
if (parent != null){
if (getChildrens(parent) == null){
List<N> ch = new ArrayList<>();
ch.add(tree);
setChildrens(ch,parent);
}else {
List<N> ch = getChildrens(parent);
ch.add(tree);
setChildrens(ch,parent);
}
}else {
newList.add(tree);
}
}
return newList;
}
}
测试:
package finTree;
import com.alibaba.fastjson.JSON;
import dto.TreeDto;
import myBaseTree.list2tree;
import java.util.ArrayList;
import java.util.List;
public class mainTest {
private static final list2tree list2tree = new list2tree<TreeDto>() {
@Override
protected String getKey(TreeDto node) {
return node.getId();
}
@Override
protected String getParentId(TreeDto node) {
return node.getPid();
}
@Override
protected List<TreeDto> getChildrens(TreeDto node) {
return node.getChildren();
}
@Override
protected void setChildrens(List<TreeDto> nodes, TreeDto node) {
node.setChildren(nodes);
}
};
public static void main(String[] args) {
TreeDto treeDto1 = new TreeDto("1","0","学生1","01");
TreeDto treeDto2 = new TreeDto("11","1","家长1","0101");
TreeDto treeDto3 = new TreeDto("12","1","家长2","0102");
TreeDto treeDto4 = new TreeDto("2","0","学生2","02");
TreeDto treeDto5 = new TreeDto("3","0","学生3","03");
TreeDto treeDto6 = new TreeDto("4","11","第3层","010101");
List<TreeDto> testTree = new ArrayList<>();
testTree.add(treeDto1);
testTree.add(treeDto2);
testTree.add(treeDto3);
testTree.add(treeDto4);
testTree.add(treeDto5);
testTree.add(treeDto6);
System.out.println(JSON.toJSON(list2tree.listToTree(testTree)));
}
}
接口式通用方法
import java.util.List;
public interface List2TreeField<T> {
String getKey(T node);
String getParentId(T node);
List<T> getChildrens(T node);
void setChildrens(List<T> nodes, T node);
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class List2TreeUtil<T> {
public List<T> runTest(List<T> oldList, List2TreeField<T> t) {
Map<String,Object> newMap = new HashMap<>();
List<T> newList = new ArrayList<>();
for (T tree : oldList){
newMap.put(t.getKey(tree),tree);
}
for (T tree : oldList){
T parent = (T) newMap.get(t.getParentId(tree));
if (parent != null){
if (t.getChildrens(parent) == null){
List<T> ch = new ArrayList<>();
ch.add(tree);
t.setChildrens(ch,parent);
}else {
List<T> ch = t.getChildrens(parent);
ch.add(tree);
t.setChildrens(ch,parent);
}
}else {
newList.add(tree);
}
}
return newList;
}
}
public static void main(String[] args) {
TreeDto treeDto1 = new TreeDto("1","0","学生1","01");
TreeDto treeDto2 = new TreeDto("11","1","家长1","0101");
TreeDto treeDto3 = new TreeDto("12","1","家长2","0102");
TreeDto treeDto4 = new TreeDto("2","0","学生2","02");
TreeDto treeDto5 = new TreeDto("3","0","学生3","03");
TreeDto treeDto6 = new TreeDto("4","11","第3层","010101");
List<TreeDto> testTree = new ArrayList<>();
testTree.add(treeDto1);
testTree.add(treeDto2);
testTree.add(treeDto3);
testTree.add(treeDto4);
testTree.add(treeDto5);
testTree.add(treeDto6);
List<TreeDto> list = new List2TreeUtil().runTest(testTree, new List2TreeField<TreeDto>() {
@Override
public String getKey(TreeDto node) {
return node.getId();
}
@Override
public String getParentId(TreeDto node) {
return node.getPid();
}
@Override
public List<TreeDto> getChildrens(TreeDto node) {
return node.getChildren();
}
@Override
public void setChildrens(List<TreeDto> nodes, TreeDto node) {
node.setChildren(nodes);
}
});
System.out.println(JSON.toJSON(list));
}