8.【原创】使用Java8中的Stream特性筛选数据
package com.fatsea.gulimall.product; import com.alibaba.fastjson.JSON; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @SpringBootTest class GulimallProductApplicationTests { class Area { // 地区编号 private Integer catId; // 地区名称 private String name; // 地区父ID private Integer parentCid; // 地区等级:2-省、3-市、4-区县、5-街道 private Integer cateLevel; // 地区排序值:越小越靠前 private Integer sort; // 子地区 List<Area> children; public Area() { } public Area(Integer catId, String name, Integer parentCid,Integer sort, Integer cateLevel) { this.catId = catId; this.name = name; this.parentCid = parentCid; this.sort = sort; this.cateLevel = cateLevel; } public Integer getCatId() { return catId; } public void setCatId(Integer catId) { this.catId = catId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getParentCid() { return parentCid; } public void setParentCid(Integer parentCid) { this.parentCid = parentCid; } public Integer getCateLevel() { return cateLevel; } public void setCateLevel(Integer cateLevel) { this.cateLevel = cateLevel; } public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } public List<Area> getChildren() { return children; } public void setChildren(List<Area> children) { this.children = children; } } @Test void testDataFilter () { // 准备数据 List<Area> allArea = new ArrayList<>(); allArea.add(new Area(1, "江西",0, 14, 2)); allArea.add(new Area(2, "湖北",0, 13, 2)); allArea.add(new Area(3, "湖南",0, 12, 2)); allArea.add(new Area(4, "九江",1, 11, 3)); allArea.add(new Area(5, "南昌",1, 10, 3)); allArea.add(new Area(6, "景德镇",1, 9, 3)); allArea.add(new Area(7, "浮梁",6, 8, 3)); allArea.add(new Area(8, "昌江",6, 7, 3)); allArea.add(new Area(9, "乐平",6, 6, 3)); allArea.add(new Area(10, "东湖区",5, 5, 4)); allArea.add(new Area(11, "南昌县",5, 4, 4)); allArea.add(new Area(12, "新建县",5, 3, 4)); allArea.add(new Area(13, "柴桑区",4, 2, 4)); allArea.add(new Area(14, "塔山街道",9, 1, 5)); // 筛选出省级地区:并排序 List<Area> provinceList = allArea.stream() .filter(area -> area.getCateLevel() == 2) .map((province) -> { // 针对单个省设置子级地区 province.setChildren(getChildrens(province, allArea)); return province; }).sorted((province1, province2) -> { return province1.getSort() - province2.getSort(); }).collect(Collectors.toList()); // 打印结果 System.out.println("areaList:" + JSON.toJSONString(provinceList)); /*[ {"catId":3,"cateLevel":2,"name":"湖南","parentCid":0,"sort":12,"children":[]}, {"catId":2,"cateLevel":2,"name":"湖北","parentCid":0,"sort":13,"children":[]}, {"catId":1,"cateLevel":2,"name":"江西","parentCid":0,"sort":14,"children": [ {"catId":6,"cateLevel":3,"name":"景德镇","parentCid":1,"sort":9,"children": [ {"catId":9,"cateLevel":3,"name":"乐平","parentCid":6,"sort":6,"children": [ {"catId":14,"cateLevel":5,"children":[],"name":"塔山街道","parentCid":9,"sort":1} ] }, {"catId":8,"cateLevel":3,"children":[],"name":"昌江","parentCid":6,"sort":7}, {"catId":7,"cateLevel":3,"children":[],"name":"浮梁","parentCid":6,"sort":8} ] }, {"catId":5,"cateLevel":3,"name":"南昌","parentCid":1,"sort":10,"children": [ {"catId":12,"cateLevel":4,"name":"新建县","parentCid":5,"sort":3,"children":[]}, {"catId":11,"cateLevel":4,"name":"南昌县","parentCid":5,"sort":4,"children":[]}, {"catId":10,"cateLevel":4,"name":"东湖区","parentCid":5,"sort":5,"children":[]} ] }, {"catId":4,"cateLevel":3,"name":"九江","parentCid":1,"sort":11,"children": [ {"catId":13,"cateLevel":4,"name":"柴桑区","parentCid":4,"sort":2,"children":[]} ] } ] } ]*/ } // 递归筛选每个省级地区的子级 public List<Area> getChildrens (Area root, List<Area> allArea) { // 筛选子级 List<Area> children = allArea.stream() // 筛选出当前地区的下一级 .filter(area -> root.getCatId() == area.getParentCid()) // 嵌套,递归查找下一级地区 .map((area) -> { area.setChildren(getChildrens(area, allArea)); return area; // 排序 }).sorted((area1, area2) -> { return area1.getSort() - area2.getSort(); }).collect(Collectors.toList()); return children; } }
同样的数据采用js的方式筛选:
var arr = [ { catId: 1, name: "江西", parentCid: 0, cateLevel: 2 }, { catId: 2, name: "湖北", parentCid: 0, cateLevel: 2 }, { catId: 3, name: "湖南", parentCid: 0, cateLevel: 2 }, { catId: 4, name: "九江", parentCid: 1, cateLevel: 3 }, { catId: 5, name: "南昌", parentCid: 1, cateLevel: 3 }, { catId: 6, name: "景德镇", parentCid: 1, cateLevel: 3 }, { catId: 7, name: "浮梁", parentCid: 6, cateLevel: 4 }, { catId: 8, name: "昌江", parentCid: 6, cateLevel: 4 }, { catId: 9, name: "乐平", parentCid: 6, cateLevel: 4 }, { catId: 10, name: "东湖区", parentCid: 5, cateLevel: 4 }, { catId: 11, name: "南昌县", parentCid: 5, cateLevel: 4 }, { catId: 12, name: "新建县", parentCid: 5, cateLevel: 4 }, { catId: 13, name: "柴桑区", parentCid: 4, cateLevel: 4 }, { catId: 14, name: "塔山街道", parentCid: 9, cateLevel: 5 } ]; arr.sort(function(a, b) { return b.cateLevel - a.cateLevel; }); console.log('arr = ', arr); var obj1 = {}; var rs_arr = []; for(var i=0; i<arr.length; i++) { var data = arr[i]; var cid = data.catId; var pid = data.parentCid; var lev = data.cateLevel; data.child = obj1[cid] ? obj1[cid] : []; if(obj1[pid]) { obj1[pid].push(data); } else { obj1[pid] = [data]; } if(2 == lev) { rs_arr.push(data) } } rs_arr.sort(function(a, b) { return a.catId - b.catId; }); console.log('rs_arr = ', rs_arr);