將一個list集合的子父節點關係的集合轉換爲一對多的關係

 /**
     * 設備位置樹形選擇器
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeDataMobile")
    public List<Location> treeDataMobile(@RequestParam(required = false) String extId, HttpServletRequest request, HttpServletResponse response) {
        EquipmentLocation equipmentLocation = new EquipmentLocation();
        if (StringUtils.isNotBlank(extId)) {
            equipmentLocation.setProject(new WindProject(extId));
        }
        List<EquipmentLocation> list = equipmentLocationService.findList(equipmentLocation);
            List<Location> locations = new ArrayList<>();
            List<Location> locationList = Lists.newArrayList();
            Map<String, Location> map = Maps.newHashMap();
            StringBuilder st = new StringBuilder("");
            for (EquipmentLocation e : list) {
                Location location = new Location(e.getId(), e.getParentId(), e.getEquipName());
                locations.add(location);
                map.put(location.getId(),location);
            }
            for (Location e : locations){
                if (map.get(e.getpId())!=null){
                     map.get(e.getpId()).getLocationList().add(e);
                }else{
                    locationList.add(e);
                }
            }
            return  locationList;
    }


    private class Location {
        private String id;
        private String pId;
        private String name;
        private Location paration;
        private List<Location> locationList =new ArrayList<>();

        Location(String id, String pId, String name) {
            this.id = id;
            this.pId = pId;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Location> getLocationList() {
            return locationList;
        }

        public void setLocationList(List<Location> locationList) {
            this.locationList = locationList;
        }

        @JsonIgnore
        public Location getParation() {
            return paration;
        }

        @JsonIgnore
        public void setParation(Location paration) {
            this.paration = paration;
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章