Java递归Java8流式处理 对父子节点结构集合数据的处理、并排序

实战

    @Test
    public void test01() {
        // 1、收集出 第一集的 数据
        List<StatisticsCenter> collect = list.stream()
                .filter((c) -> c.getParentNum() == 0)
                // 与map功能一致,对list中每一项循环转化,封装childrenList
                .peek((c) -> c.setChildren(getChildrenList(c.getNum(), list)))
                //排序操作
                .sorted(Comparator.comparingInt(StatisticsCenter::getTotal))
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
    }

    //2、获取子节点集合
    public List<StatisticsCenter> getChildrenList(Integer pNum, List<StatisticsCenter> old) {
        return old.stream()
                .filter((c) -> pNum.equals(c.getParentNum()))
                .peek((c) -> c.setChildren(getChildrenList(c.getNum(), old)))
                .sorted(Comparator.comparingInt(StatisticsCenter::getTotal))
                .collect(Collectors.toList());
    }

@Data
@Accessors(chain = true)
class StatisticsCenter implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private Integer num;
    private String title;
    private Integer parentNum;
    private Integer total;
    private List<StatisticsCenter> children;
}

实测,可以使用以下数据进行测试

数据准备:

sql 可以去 码云上 获取: 

https://gitee.com/naimaohome/talk_about_fage.git

数据封装:

    private static List<StatisticsCenter> list = new ArrayList<>();
    private Connection connection;
    private PreparedStatement statement;
    private ResultSet rs;

    @Before
    public void getAllData() throws SQLException {
        connection = DbUtil.getMysqlConnection();
        statement = connection.prepareStatement("select * from ba_statistics_center");
        rs = statement.executeQuery();
        while (rs.next()) {
            final StatisticsCenter center = new StatisticsCenter();
            center.setId(rs.getInt("id"))
                    .setNum(rs.getInt("num"))
                    .setTitle(rs.getString("title"))
                    .setParentNum(rs.getInt("parent_num"))
                    .setTotal(rs.getInt("total"));
            list.add(center);
        }
    }

    @After
    public void closeResource() throws SQLException {
        rs.close();
        statement.close();
        connection.close();
    }

    public static Connection getMysqlConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://192.168.0.44:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
            String username = "root";
            String password = "xxx";
            conn = DriverManager.getConnection(url, username, password);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

航迹者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值