| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package cn.com.goldenwater.dcproj.utils;
- import cn.com.goldenwater.dcproj.model.Menu;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
- /**
- * 菜单管理,对菜单进行树状排序
- */
- public class RecursiveSort{
- //输出列表
- private List<Menu> resultList = new ArrayList<>();
- //输入列表
- private List<Menu> intoList;
- /**
- * 排序
- * @param intoList 输入列表
- */
- private RecursiveSort(List<Menu> intoList){
- this.intoList = intoList;
- for(Menu t : this.intoList){
- // 顶级类父ID为‘0’
- String PARENT_ID = "0";
- if(PARENT_ID.equals(t.getParentId())){
- // 当父级为 0 时即顶级,首先放入输出列表
- resultList.add(t);
- // 查询下级
- findChildren(t);
- }
- }
- }
- /**
- * 查询下级
- * @param t
- */
- private void findChildren(Menu t){
- List<Menu> childrenList = new ArrayList<>();
- //遍历输入列表,查询下级
- for(Menu d : intoList){
- if(Objects.equals(t.getId(), d.getParentId())) {
- childrenList.add(d);
- }
- }
- //遍历到最末端,无下级,退出遍历
- if(childrenList.isEmpty()){
- return;
- }
- //对下级进行遍历
- for(Menu d : childrenList){
- resultList.add(d);
- findChildren(d);
- }
- }
- private List<Menu> getResultList(){
- return resultList;
- }
- public static List<Menu> sort(List<Menu> originalList){
- return new RecursiveSort(originalList).getResultList();
- }
- }
|