树形结构筛选:集合result为List树形结构,数组arr为所需要获取的树形结构各级code

实现代码

package com.test;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @classname: DemoTest
 * @description:
 * @author: YJ
 * @create: 2021/09/28
 */
public class DemoTest {
    public static void main(String[] args) {
        int[] arr = {1,12};
        List<Demo> result = getDemoList();

        List<Demo> resultTemp = null;
        for (int i=0; i<arr.length; i++) {
            int item = arr[i];
            if (resultTemp == null) {
                Iterator<Demo> iterator = result.iterator();
                while (iterator.hasNext()) {
                    Demo iteratorItem = iterator.next();
                    if (item != iteratorItem.getCode()) {
                        //移除不同的数据
                        iterator.remove();
                    }
                }
                if (CollectionUtils.isNotEmpty(result)) {
                    resultTemp = result.get(0).getChild();
                }
            }else {
                Iterator<Demo> iterator = resultTemp.iterator();
                while (iterator.hasNext()) {
                    Demo iteratorItem = iterator.next();
                    if (item != iteratorItem.getCode()) {
                        //移除不同的数据
                        iterator.remove();
                    }
                }
                if (CollectionUtils.isNotEmpty(resultTemp)) {
                    resultTemp = resultTemp.get(0).getChild();
                }
            }
        }
        System.out.println(JSON.toJSON(result));
    }
    private static List<Demo> getDemoList() {
        Demo C111 = new Demo("C111", 111, new ArrayList<>()),
                C112 = new Demo("C112", 112, new ArrayList<>()),
                C113 = new Demo("C113", 113, new ArrayList<>()),
                C114 = new Demo("C114", 114, new ArrayList<>()),
                C121 = new Demo("C121", 121, new ArrayList<>()),
                C311 = new Demo("C311", 311, new ArrayList<>());
        List<Demo> listB1 = new ArrayList<>(),
                listB2 = new ArrayList<>(),
                listB3 = new ArrayList<>();
        listB1.add(C111);
        listB1.add(C112);
        listB1.add(C113);
        listB1.add(C114);
        listB2.add(C121);
        listB3.add(C311);
        Demo B11 = new Demo("B11", 11, listB1),
                B12 = new Demo("B12", 12, listB2),
                B21 = new Demo("B21", 21, new ArrayList<>()),
                B22 = new Demo("B22", 22, new ArrayList<>()),
                B23 = new Demo("B23", 23, new ArrayList<>()),
                B31 = new Demo("B31", 31, listB3);

        List<Demo> listA1 = new ArrayList<>(),
                listA2 = new ArrayList<>(),
                listA3 = new ArrayList<>();
        listA1.add(B11);
        listA1.add(B12);
        listA2.add(B21);
        listA2.add(B22);
        listA2.add(B23);
        listA3.add(B31);
        Demo A1 = new Demo("A1", 1, listA1),
                A2 = new Demo("A2", 2, listA2),
                A3 = new Demo("A3", 3, listA3);
        List<Demo> result = new ArrayList<>();
        result.add(A1);
        result.add(A2);
        result.add(A3);

        return result;
    }
}

@Data
class Demo {
    private String name;

    private int code;

    private List<Demo> child;

    public Demo() {
    }

    public Demo(String name, int code, List<Demo> child) {
        this.name = name;
        this.code = code;
        this.child = child;
    }
}

树形结构result

[
    {
        "code":1,
        "name":"A1",
        "child":[
            {
                "code":11,
                "name":"B11",
                "child":Array[4]
            },
            {
                "code":12,
                "name":"B12",
                "child":[
                    {
                        "code":121,
                        "name":"C121",
                        "child":[

                        ]
                    }
                ]
            }
        ]
    },
    {
        "code":2,
        "name":"A2",
        "child":[
            {
                "code":21,
                "name":"B21",
                "child":[

                ]
            },
            {
                "code":22,
                "name":"B22",
                "child":[

                ]
            },
            {
                "code":23,
                "name":"B23",
                "child":[

                ]
            }
        ]
    },
    {
        "code":3,
        "name":"A3",
        "child":[
            {
                "code":31,
                "name":"B31",
                "child":[
                    {
                        "code":311,
                        "name":"C311",
                        "child":[

                        ]
                    }
                ]
            }
        ]
    }
]

测试


arr = {1,12};

[
    {
        "code":1,
        "name":"A1",
        "child":[
            {
                "code":12,
                "name":"B12",
                "child":[
                    {
                        "code":121,
                        "name":"C121",
                        "child":[

                        ]
                    }
                ]
            }
        ]
    }
]

arr = {3,31,311};

[
    {
        "code":3,
        "name":"A3",
        "child":[
            {
                "code":31,
                "name":"B31",
                "child":[
                    {
                        "code":311,
                        "name":"C311",
                        "child":[

                        ]
                    }
                ]
            }
        ]
    }
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值