1. import java.util.List;
    2. public class TreeDto {
    3. public TreeDto(String id, String pid, String name, String treeNo){
    4. this.id=id;
    5. this.pid = pid;
    6. this.name = name;
    7. this.treeNo = treeNo;
    8. }
    9. private String id;
    10. private String pid;//fatherid
    11. private String name;
    12. private String treeNo;
    13. private List<TreeDto> children;
    14. //省略get和set
    15. }
    1. import dto.TreeDto;
    2. import com.alibaba.fastjson.JSON;
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7. public class listToTreeTest {
    8. public static void main(String[] args) {
    9. TreeDto treeDto1 = new TreeDto("1","0","学生1","01");
    10. TreeDto treeDto2 = new TreeDto("11","1","家长1","0101");
    11. TreeDto treeDto3 = new TreeDto("12","1","家长2","0102");
    12. TreeDto treeDto4 = new TreeDto("2","0","学生2","02");
    13. TreeDto treeDto5 = new TreeDto("3","0","学生3","03");
    14. TreeDto treeDto6 = new TreeDto("4","11","第3层","010101");
    15. List<TreeDto> testTree = new ArrayList<>();
    16. testTree.add(treeDto1);
    17. testTree.add(treeDto2);
    18. testTree.add(treeDto3);
    19. testTree.add(treeDto4);
    20. testTree.add(treeDto5);
    21. testTree.add(treeDto6);
    22. System.out.println(JSON.toJSON(listToTree(testTree)));
    23. }
    24. // 列表转树结构
    25. private static List<TreeDto> listToTree(List<TreeDto> oldList){
    26. Map<String,Object> newMap = new HashMap<>();
    27. List<TreeDto> newList = new ArrayList<>();
    28. for (TreeDto treeDto : oldList){
    29. newMap.put(treeDto.getId(),treeDto);
    30. }
    31. for (TreeDto treeDto : oldList){
    32. TreeDto parent = (TreeDto) newMap.get(treeDto.getPid());
    33. if (parent != null){
    34. if (parent.getChildren() == null){
    35. List<TreeDto> ch = new ArrayList<>();
    36. ch.add(treeDto);
    37. parent.setChildren(ch);
    38. }else {
    39. List<TreeDto> ch = parent.getChildren();
    40. ch.add(treeDto);
    41. parent.setChildren(ch);
    42. }
    43. }else {
    44. newList.add(treeDto);
    45. }
    46. }
    47. return newList;
    48. }
    49. }

    把方法里面需要用到的字段放到父类里面,需要转成树的类继承父类,就能改成通用的。考虑到如果需要转换的类都已经继承了某一公共类不能再继承了,改成抽象,抽象出id,父id和子集,重写就能用。

    1. package myBaseTree;
    2. import java.util.ArrayList;
    3. import java.util.HashMap;
    4. import java.util.List;
    5. import java.util.Map;
    6. public abstract class list2tree<N> {
    7. /**
    8. * 获取节点唯一标识方法
    9. * @param node
    10. * @return
    11. */
    12. protected abstract String getKey(N node);
    13. /**
    14. * 获取节点父节点唯一标识方法
    15. * @param node
    16. * @return
    17. */
    18. protected abstract String getParentId(N node);
    19. /**
    20. * 子节点
    21. * @param node
    22. * @return
    23. */
    24. protected abstract List<N> getChildrens(N node);
    25. protected abstract void setChildrens(List<N> nodes,N node);
    26. /**
    27. * 生成树方法
    28. * @param oldList
    29. * @return
    30. */
    31. //@Override
    32. public List<N> listToTree(List<N> oldList) {
    33. Map<String,Object> newMap = new HashMap<>();
    34. List<N> newList = new ArrayList<>();
    35. for (N tree : oldList){
    36. newMap.put(getKey(tree),tree);
    37. }
    38. for (N tree : oldList){
    39. N parent = (N) newMap.get(getParentId(tree));
    40. if (parent != null){
    41. if (getChildrens(parent) == null){
    42. List<N> ch = new ArrayList<>();
    43. ch.add(tree);
    44. setChildrens(ch,parent);
    45. }else {
    46. List<N> ch = getChildrens(parent);
    47. ch.add(tree);
    48. setChildrens(ch,parent);
    49. }
    50. }else {
    51. newList.add(tree);
    52. }
    53. }
    54. return newList;
    55. }
    56. }

    测试:

    1. package finTree;
    2. import com.alibaba.fastjson.JSON;
    3. import dto.TreeDto;
    4. import myBaseTree.list2tree;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. public class mainTest {
    8. private static final list2tree list2tree = new list2tree<TreeDto>() {
    9. @Override
    10. protected String getKey(TreeDto node) {
    11. return node.getId();
    12. }
    13. @Override
    14. protected String getParentId(TreeDto node) {
    15. return node.getPid();
    16. }
    17. @Override
    18. protected List<TreeDto> getChildrens(TreeDto node) {
    19. return node.getChildren();
    20. }
    21. @Override
    22. protected void setChildrens(List<TreeDto> nodes, TreeDto node) {
    23. node.setChildren(nodes);
    24. }
    25. };
    26. public static void main(String[] args) {
    27. TreeDto treeDto1 = new TreeDto("1","0","学生1","01");
    28. TreeDto treeDto2 = new TreeDto("11","1","家长1","0101");
    29. TreeDto treeDto3 = new TreeDto("12","1","家长2","0102");
    30. TreeDto treeDto4 = new TreeDto("2","0","学生2","02");
    31. TreeDto treeDto5 = new TreeDto("3","0","学生3","03");
    32. TreeDto treeDto6 = new TreeDto("4","11","第3层","010101");
    33. List<TreeDto> testTree = new ArrayList<>();
    34. testTree.add(treeDto1);
    35. testTree.add(treeDto2);
    36. testTree.add(treeDto3);
    37. testTree.add(treeDto4);
    38. testTree.add(treeDto5);
    39. testTree.add(treeDto6);
    40. System.out.println(JSON.toJSON(list2tree.listToTree(testTree)));
    41. }
    42. }

    接口式通用方法

    1. import java.util.List;
    2. public interface List2TreeField<T> {
    3. String getKey(T node);
    4. String getParentId(T node);
    5. List<T> getChildrens(T node);
    6. void setChildrens(List<T> nodes, T node);
    7. }
    1. import java.util.ArrayList;
    2. import java.util.HashMap;
    3. import java.util.List;
    4. import java.util.Map;
    5. public class List2TreeUtil<T> {
    6. public List<T> runTest(List<T> oldList, List2TreeField<T> t) {
    7. Map<String,Object> newMap = new HashMap<>();
    8. List<T> newList = new ArrayList<>();
    9. for (T tree : oldList){
    10. newMap.put(t.getKey(tree),tree);
    11. }
    12. for (T tree : oldList){
    13. T parent = (T) newMap.get(t.getParentId(tree));
    14. if (parent != null){
    15. if (t.getChildrens(parent) == null){
    16. List<T> ch = new ArrayList<>();
    17. ch.add(tree);
    18. t.setChildrens(ch,parent);
    19. }else {
    20. List<T> ch = t.getChildrens(parent);
    21. ch.add(tree);
    22. t.setChildrens(ch,parent);
    23. }
    24. }else {
    25. newList.add(tree);
    26. }
    27. }
    28. return newList;
    29. }
    30. }
    1. public static void main(String[] args) {
    2. TreeDto treeDto1 = new TreeDto("1","0","学生1","01");
    3. TreeDto treeDto2 = new TreeDto("11","1","家长1","0101");
    4. TreeDto treeDto3 = new TreeDto("12","1","家长2","0102");
    5. TreeDto treeDto4 = new TreeDto("2","0","学生2","02");
    6. TreeDto treeDto5 = new TreeDto("3","0","学生3","03");
    7. TreeDto treeDto6 = new TreeDto("4","11","第3层","010101");
    8. List<TreeDto> testTree = new ArrayList<>();
    9. testTree.add(treeDto1);
    10. testTree.add(treeDto2);
    11. testTree.add(treeDto3);
    12. testTree.add(treeDto4);
    13. testTree.add(treeDto5);
    14. testTree.add(treeDto6);
    15. List<TreeDto> list = new List2TreeUtil().runTest(testTree, new List2TreeField<TreeDto>() {
    16. @Override
    17. public String getKey(TreeDto node) {
    18. return node.getId();
    19. }
    20. @Override
    21. public String getParentId(TreeDto node) {
    22. return node.getPid();
    23. }
    24. @Override
    25. public List<TreeDto> getChildrens(TreeDto node) {
    26. return node.getChildren();
    27. }
    28. @Override
    29. public void setChildrens(List<TreeDto> nodes, TreeDto node) {
    30. node.setChildren(nodes);
    31. }
    32. });
    33. System.out.println(JSON.toJSON(list));
    34. }