栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java使用stream流构建自定义树形结构

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用stream流构建自定义树形结构

实现效果:

查询基础数据做准备: 

 java实现:

package com.aa.web.controller.tree;

import com.alibaba.fastjson.JSONObject;
import com.aa.common.core.domain.AjaxResult;
import com.aa.system.domain.tree.MonitorCar;
import com.aa.system.domain.tree.MonitorParams;
import com.aa.system.domain.tree.MonitorTree;
import com.aa.system.service.IClJichuxxService;
import com.aa.system.service.ISysDeptService;
import com.aa.web.controller.tool.testMapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
public class MonitorTreeController {

    @Autowired
    ISysDeptService sysDeptService;
    @Autowired
    IClJichuxxService cljichuxxService;

    
    @PostMapping("/monitor/tree")
    public AjaxResult getMonitorTree(@RequestBody String params) throws Exception {
        JSonObject jsonObject = JSONObject.parseObject(params);
        String sign = jsonObject.getString("sign");
        String carType = jsonObject.getString("carType");
        try {
            MonitorParams params1 = new MonitorParams();
            params1.setSign(sign);
            params1.setCarType(carType);
            List deptlist = sysDeptService.getMonitorTree(params1);
            List deptlistwithcars = deptlist.stream()
                    .map(item -> {
                        List carsByDeptId = cljichuxxService.getCarsByDeptId(item.getId()).stream()
                                .map(caritem -> {
                                    if ("car".equals(caritem.getFlag())) {
                                        String online = caritem.getonline();
                                        switch(online){
                                            case "0" :
                                                caritem.setonline("离线");
                                                break;
                                            case "1" :
                                                caritem.setonline("在线");
                                                break;
                                            default :
                                                break;
                                        }
                                        String city= caritem.getCity();
                                        caritem.setTotal(city);
                                    }
                                    return caritem;
                                }).collect(Collectors.toList());
                        item.setChildren(carsByDeptId);
                        item.setTotal(carsByDeptId.size()+"");
                        List carsonline = carsByDeptId.stream()
                                .filter(item1 -> "在线".equals(item1.getonline())).collect(Collectors.toList());
                        item.setonline(carsOnline.size()+"");
                        return item;
                    })
                    .collect(Collectors.toList());
            List result = deptlistwithcars.stream()
                    .filter(item -> getRoot(item,deptlistwithcars))
                    .map(item -> {
                        item.setTotal(gettotal(item, deptlistwithcars));
                        item.setonline(getonline(item,deptlistwithcars));
                        List childrencar = item.getChildren();
                        List childrendept = getChildren(item, deptlistwithcars);
                        childrendept.addAll(childrencar);
                        item.setChildren(childrendept);
                        return item;
                    })
                    .collect(Collectors.toList());
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    
    private boolean getRoot(MonitorTree item, List list) {
        List arrayList = new ArrayList();
        for (MonitorTree ele : list) {
            arrayList.add(ele.getId());
        }
        return  !arrayList.contains(item.getPcode());
    }

    
    public List getChildren(MonitorTree root, List all) {
        List children = all.stream()
            .filter(item -> item.getPcode().equals(root.getCode()))
            .map(item -> {
                List childrencar = item.getChildren();
                List childrendept = getChildren(item, all);
                childrendept.addAll(childrencar);
                item.setChildren(childrendept);
                return item;
            }).collect(Collectors.toList());
        return children;
    }

    
    private String gettotal(MonitorTree root, List all) {
        int total = Integer.parseInt(root.getTotal());//先取本身的总数
        for (MonitorTree item : all){
            if(item.getPcode().equals(root.getCode())){//循环找下级
                item.setTotal(gettotal(item,all));
                int i = Integer.parseInt(item.getTotal());
                total += i;
            }
        }
        return  String.valueOf(total);
    }

    
    private String getonline(MonitorTree root, List all) {
        int online = Integer.parseInt(root.getonline());
        for (MonitorTree item : all){
            if(item.getPcode().equals(root.getCode())){//循环找下级
                item.setonline(getonline(item,all));
                int i = Integer.parseInt(item.getonline());
                online += i;
            }
        }
        return  String.valueOf(online);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/703761.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号