简单封装一个类似菜单栏的树状结构转换

充血的菜单实体类

@Data
public class Menu {
    public Integer id;
    public String name;
    public Integer parentId;// 根节点为0
    public List<Menu> childList;

    public Menu(Integer id, String name, Integer parentId) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
        this.childList = new ArrayList<>();
    }

    public static List<Menu> selectAll() {
        return Arrays.asList(
            new Menu(1, "根节点", 0),
            new Menu(2, "子节点1", 1),
            new Menu(3, "子节点1.1", 2),
            new Menu(4, "子节点1.2", 2),
            new Menu(5, "根节点1.3", 2),
            new Menu(6, "根节点2", 1),
            new Menu(7, "根节点2.1", 6),
            new Menu(8, "根节点2.2", 6),
            new Menu(9, "根节点2.2.1", 7),
            new Menu(10, "根节点2.2.2", 7),
            new Menu(11, "根节点3", 1),
            new Menu(12, "根节点3.1", 11)
        );
    }
}

先做实现

public class Test {
	public static void main(String[] args) {
		List<Menu> menuList = Menu.selectAll();

		// 1:遍历(O(n))节点并查找(O(1))加入父节点。总复杂度为O(n)。
		Map<Integer, Menu> menuMap = menuList.stream().collect(Collectors.toMap(Menu::getId, menu -> menu));
		menuMap.forEach((key, value) -> {
			if (value.getParentId() == 0) return; // 根节点不处理
			menuMap.get(value.getParentId()).getChildList().add(value);
		});
		Menu root = menuMap.get(1);
        
		System.out.println(root);
	}
}

封装一下,主要抽象了实体类的ID、父节点ID、子节点列表这三个字段的Getter

public class Test {
	public static void main(String[] args) {
		List<Menu> menuList = Menu.selectAll();

        // 抽象
		class Tree<T>{
			public T parse(List<T> list, Function<T, Integer> getId, Function<T,Integer> getParentId, Function<T, Collection<T>> getChildList) {
				Map<Integer, T> map = list.stream().collect(Collectors.toMap(getId, t -> t));
				map.forEach((key, value) -> {
					if (getParentId.apply(value) == 0) return; // 根节点不处理
					getChildList.apply(map.get(getParentId.apply(value))).add(value);
				});
				return map.get(1);
			}
		}
		Menu root = new Tree<Menu>().parse(menuList, Menu::getId, Menu::getParentId, Menu::getChildList);

		System.out.println(root);
	}
}

再封装一下,把根节点的判断条件封装了

public class Test {
	public static void main(String[] args) {
		List<Menu> menuList = Menu.selectAll();

        // 抽象
		class Tree<T>{
			public T parse(List<T> list, Function<T, Integer> getId, Function<T,Integer> getParentId, Function<T, Collection<T>> getChildList,Function<T,Boolean> isRoot) {
				AtomicReference<T> root = new AtomicReference<>();
				Map<Integer, T> map = list.stream().collect(Collectors.toMap(getId, t -> t));
				map.forEach((key, value) -> {
					if (isRoot.apply(value)) {
						root.set(value);
						return; // 根节点不处理
					}
					getChildList.apply(map.get(getParentId.apply(value))).add(value);
				});
				return root.get();
			}
		}
		Menu root = new Tree<Menu>().parse(menuList, Menu::getId, Menu::getParentId, Menu::getChildList, menu -> menu.getParentId() == 0);

		System.out.println(root);
	}
}

再再封装,类泛型有点大,改为方法泛型吧

public class Test {
	public static void main(String[] args) {
		List<Menu> menuList = Menu.selectAll();
		
        // 抽象
		class Tree {
			public static <T> T parse(List<T> list, Function<T, Integer> getId, Function<T,Integer> getParentId, Function<T, Collection<T>> getChildList,Function<T,Boolean> isRoot) {
				AtomicReference<T> root = new AtomicReference<>();
				Map<Integer, T> map = list.stream().collect(Collectors.toMap(getId, t -> t));
				map.forEach((key, value) -> {
					if (isRoot.apply(value)) {
						root.set(value);
						return; // 根节点不处理
					}
					getChildList.apply(map.get(getParentId.apply(value))).add(value);
				});
				return root.get();
			}
		}
		Menu root = Tree.parse(menuList, Menu::getId, Menu::getParentId, Menu::getChildList, menu -> menu.getParentId() == 0);

		System.out.println(root);
	}
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值