赞
踩
问题:
1.数据量大,JSON 串复杂
2.数据转换实体字段命名名不一致,但是数据类型一样。
解决方案:
- public static void main(String[] args) {
- String json = "[{\"name\":\"A\",\"children\":[{\"name\":\"B\",\"children\":[{\"name\":\"D\",\"children\":[{\"name\":\"G\"},{\"name\":\"H\"}]}]},{\"name\":\"C\",\"children\":[{\"name\":\"E\",\"children\":[{\"name\":\"I\"}]},{\"name\":\"F\"}]}]}]";
- List<TreeNode> treeNodes = JSON.parseArray(json, TreeNode.class);
- System.out.println(flatTree(treeNodes).stream().map(TreeNode2::getCode).collect(Collectors.toList()));
- System.out.println(flatTree(treeNodes));
- }
-
- //JSON 展平
- private static List<TreeNode2> flatTree(List<TreeNode> tree) {
- return Optional.ofNullable(tree).map(t -> t.stream()
- .flatMap(node ->
- {
- //类型转换
- List<TreeNode2> children = flatTree(node.getChildren());
- return Stream.concat(Stream.of(new TreeNode2(node.getName(),children)),children.stream());
- }
- )
- .collect(Collectors.toList())).orElse(Collections.emptyList());
- }
-
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- class TreeNode {
- private String name;
- private List<TreeNode> children;
- }
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- class TreeNode2 {
- private String code;
- private List<TreeNode2> children;
- }

前端代码演示 DEMO
- // A
- // / \
- // B C
- // / \ / \
- // D E F G
- // const tree1 = [
- // {
- // name: "A",
- // children: [
- // { name: "B", children: [{ name: "D" }, { name: "E" }] },
- // { name: "C", children: [{ name: "F" }, { name: "G" }] },
- // ],
- // },
- // ];
-
- // A
- // / \
- // B C
- // / / \
- // D E F
- // / \ \
- // G H I
- const tree = [
- {
- name: "A",
- children: [
- {
- name: "B",
- children: [{ name: "D", children: [{ name: "G" }, { name: "H" }] }],
- },
- {
- name: "C",
- children: [{ name: "E", children: [{ name: "I" }] }, { name: "F" }],
- },
- ],
- },
- ];
-
- // // 递归
- // function loopTree(tree) {
- // (tree || []).forEach((item) => {
- // // 前序遍历
- // // console.log(item.name);
- // loopTree(item.children);
- // // 后续遍历
- // console.log(item.name);
- // });
- // }
- // loopTree(tree);
- // console.log("------------------");
- // function sTree(tree) {
- // const arr = [...(tree || [])];
- // while (arr.length) {
- // // const item = arr.pop(); //前序遍历
- // const item = arr.shift(); //层级遍历
- // console.log(item.name);
- // arr.push(...(item.children || []));
- // // arr.push(...(item.children || []).reverse());
- // }
- // }
- // sTree(tree);
- // console.log("------------------");
- function flatTree(tree) {
- return (tree || []).flatMap((node) => [...flatTree(node.children), node]);
- }
- console.log(
- flatTree(tree)
- .map((v) => v.name)
- .join()
- );
-
- // 广度遍历 - 层级遍历
- // 深度遍历
- // 前序遍历(跟左右)
- // 中序遍历(左跟右) 自己了解
- // 后序遍历(左右跟)
-
- // len > 0 size > 0
- // 当 len = 1 size = 1
- // [0]
- // 当 len = 2 size = 1
- // [0, 0]
- // 当 len = 3 size = 1
- // [0, 0, 0]
- // 当 len = 2 size = 2
- // [0, 0]
- // [0, 1]
- // [1, 0]
- // [1, 1]
- // 当 len = 2 size = 3
- // [0, 0]
- // [0, 1]
- // [0, 2]
- // [1, 0]
- // [1, 1]
- // [1, 2]
- // [2, 0]
- // [2, 1]
- // [2, 2]
- // 当 len = 3 size = 2
- // [0, 0, 0]
- // [0, 0, 1]
- // [0, 1, 0]
- // [0, 1, 1]
- // [1, 0, 0]
- // [1, 0, 1]
- // [1, 1, 0]
- // [1, 1, 1]
- // 当 len = 3 size = 3
- // [0, 0, 0]
- // [0, 0, 1]
- // [0, 0, 2]
- // [0, 1, 0]
- // [0, 1, 1]
- // [0, 1, 2]
- // [0, 2, 0]
- // [0, 2, 1]
- // [0, 2, 2]
- // [1, 0, 0]
- // [1, 0, 1]
- // [1, 0, 2]
- // [1, 1, 0]
- // [1, 1, 1]
- // [1, 1, 2]
- // [1, 2, 0]
- // [1, 2, 1]
- // [1, 2, 2]
- // [2, 0, 0]
- // [2, 0, 1]
- // [2, 0, 2]
- // [2, 1, 0]
- // [2, 1, 1]
- // [2, 1, 2]
- // [2, 2, 0]
- // [2, 2, 1]
- // [2, 2, 2]

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。