5de3e0a2dbbece66dc7dd2f66401a6abbf4cc17e.svn-base 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package cn.com.goldenwater.dcproj.utils;
  2. import cn.com.goldenwater.dcproj.model.Menu;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Objects;
  6. /**
  7. * 菜单管理,对菜单进行树状排序
  8. */
  9. public class RecursiveSort{
  10. //输出列表
  11. private List<Menu> resultList = new ArrayList<>();
  12. //输入列表
  13. private List<Menu> intoList;
  14. /**
  15. * 排序
  16. * @param intoList 输入列表
  17. */
  18. private RecursiveSort(List<Menu> intoList){
  19. this.intoList = intoList;
  20. for(Menu t : this.intoList){
  21. // 顶级类父ID为‘0’
  22. String PARENT_ID = "0";
  23. if(PARENT_ID.equals(t.getParentId())){
  24. // 当父级为 0 时即顶级,首先放入输出列表
  25. resultList.add(t);
  26. // 查询下级
  27. findChildren(t);
  28. }
  29. }
  30. }
  31. /**
  32. * 查询下级
  33. * @param t
  34. */
  35. private void findChildren(Menu t){
  36. List<Menu> childrenList = new ArrayList<>();
  37. //遍历输入列表,查询下级
  38. for(Menu d : intoList){
  39. if(Objects.equals(t.getId(), d.getParentId())) {
  40. childrenList.add(d);
  41. }
  42. }
  43. //遍历到最末端,无下级,退出遍历
  44. if(childrenList.isEmpty()){
  45. return;
  46. }
  47. //对下级进行遍历
  48. for(Menu d : childrenList){
  49. resultList.add(d);
  50. findChildren(d);
  51. }
  52. }
  53. private List<Menu> getResultList(){
  54. return resultList;
  55. }
  56. public static List<Menu> sort(List<Menu> originalList){
  57. return new RecursiveSort(originalList).getResultList();
  58. }
  59. }