java lambda 多级分组,支持实体对象和json对象两种方式

   最近做到一个项目,需要对数据进行多级分组,既要支持实体对象,也要只要json这种格式的数据,总结下这两种方式对应的Java lamda写法。

下面直接上代码演示:

package com.fh.iasp.platform.sm.logon.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;

/**
 * @Description Java集合多重分组---lambda表达式实现
 * @Version 1.0
 **/
@RestController
@RequestMapping("group")
public class GroupController {

    /**
     * 国家对象
     */
    @Data
    @AllArgsConstructor
    static class Country {
        /**
         * 省份ID
         */
        Integer pid;
        /**
         * 省份名称
         */
        String pname;
        /**
         * 城市ID
         */
        Integer cid;
        /**
         * 城市名称
         */
        String cname;
        /**
         * 地区ID
         */
        Integer did;
        /**
         * 地区名称
         */
        String dname;

    }

    /**
     * 创建测试数据(实际数据是从数据库查询)
     */
    private static List list = new ArrayList<>(10);

    static {
        list.add(new Country(1, "江苏", 2, "南京市", 11, "玄武区"));
        list.add(new Country(1, "江苏", 2, "南京市", 12, "鼓楼区"));
        list.add(new Country(1, "江苏", 2, "南京市", 13, "江北区"));
        list.add(new Country(1, "江苏", 2, "淮安市", 24, "淮阴区"));
        list.add(new Country(1, "江苏", 2, "淮安市", 25, "楚州区"));
        list.add(new Country(1, "江苏", 2, "宿迁市", 31, "泗阳县"));
        list.add(new Country(1, "江苏", 2, "宿迁市", 32, "泗洪县"));
        list.add(new Country(1, "江苏", 2, "南通市", 41, "海安市"));
        list.add(new Country(2, "浙江", 7, "杭州市", 51, "上城区"));
        list.add(new Country(2, "浙江", 7, "杭州市", 51, "下城区"));
        list.add(new Country(2, "浙江", 7, "湖州市", 52, "南浔区"));
        list.add(new Country(2, "浙江", 7, "宁波市", 54, "江北区"));
        list.add(new Country(2, "浙江", 7, "衢州市", 55, "衢江区"));
        list.add(new Country(5, "山东", 9, "济南市", 56, "市南区"));
        list.add(new Country(5, "山东", 9, "潍坊市", 57, "奎文区"));
    }

    public static void main(String[] args)
    {
        list();
        groupBy();
        grouping("pname","cname");
    }

    //通过条件处理后分组
    public static Map list() {
        // 返回前端结果集
        Map>> map = new HashMap<>(16);

        // lambda进行多重分组
        list.stream().collect(groupingBy(c -> {
            // 因为groupingBy函数的参数不能为null  校验pname是否为空
            String pname = c.getPname();
            if (StringUtils.isBlank(pname)) {
                pname = "其他";
            }
            return pname;
        })).forEach((n, l) -> {
            Map> collect = l.stream()
                    .collect(groupingBy(c -> {
                        // 因为groupingBy函数的参数不能为null  校验cname是否为空
                        String cname = c.getCname();
                        if (StringUtils.isBlank(cname)) {
                            cname = "其他";
                        }
                        return cname;
                    }, mapping(Country::getDname, toList())));
            map.put(n, collect);
        });
        return map;
    }

    //直接分组
    public static Map groupBy(){
        Map>> collect = list.stream()
                .collect(groupingBy(Country::getPname, Collectors.groupingBy(Country::getCname)));
        return collect;
    }

    //支持json,同时支持传对应要分组的key值
    public static Map grouping(String groupA,String groupB){
        JSONArray array = new JSONArray();
        JSONObject xw = new JSONObject();
        xw.put("pname","江苏");
        xw.put("cname","南京市");
        xw.put("dname","玄武区");
        array.add(xw);
        JSONObject gl = new JSONObject();
        gl.put("pname","江苏");
        gl.put("cname","南京市");
        gl.put("dname","鼓楼区");
        array.add(gl);
        JSONObject jb = new JSONObject();
        jb.put("pname","江苏");
        jb.put("cname","南京市");
        jb.put("dname","江北区");
        array.add(jb);
        JSONObject hy = new JSONObject();
        hy.put("pname","江苏");
        hy.put("cname","淮安市");
        hy.put("dname","淮阴区");
        array.add(hy);
        JSONObject cz = new JSONObject();
        cz.put("pname","江苏");
        cz.put("cname","淮安市");
        cz.put("dname","楚州区");
        array.add(cz);
        JSONObject sy = new JSONObject();
        sy.put("pname","江苏");
        sy.put("cname","宿迁市");
        sy.put("dname","泗阳县");
        array.add(sy);
        JSONObject sh = new JSONObject();
        sh.put("pname","江苏");
        sh.put("cname","宿迁市");
        sh.put("dname","泗洪县");
        array.add(sh);
        JSONObject ha = new JSONObject();
        ha.put("pname","江苏");
        ha.put("cname","南通市");
        ha.put("dname","海安市");
        array.add(ha);
        JSONObject sc = new JSONObject();
        sc.put("pname","浙江");
        sc.put("cname","杭州市");
        sc.put("dname","上城区");
        array.add(sc);
        JSONObject xc = new JSONObject();
        xc.put("pname","浙江");
        xc.put("cname","杭州市");
        xc.put("dname","下城区");
        array.add(xc);
        JSONObject nx = new JSONObject();
        nx.put("pname","浙江");
        nx.put("cname","湖州市");
        nx.put("dname","南浔区");
        array.add(nx);
        JSONObject zjb = new JSONObject();
        zjb.put("pname","浙江");
        zjb.put("cname","宁波市");
        zjb.put("dname","江北区");
        array.add(zjb);
        JSONObject qj = new JSONObject();
        qj.put("pname","浙江");
        qj.put("cname","衢州市");
        qj.put("dname","衢江区");
        array.add(qj);
        JSONObject sn = new JSONObject();
        sn.put("pname","山东");
        sn.put("cname","济南市");
        sn.put("dname","市南区");
        array.add(sn);
        JSONObject kw = new JSONObject();
        kw.put("pname","山东");
        kw.put("cname","潍坊市");
        kw.put("dname","奎文区");
        array.add(kw);
        //此处方便后面直接写成轮子,array也作为一个参数传进来,所以判断下是否为空
        if(array!=null && !array.isEmpty()){
            Map>> collect = array.stream().collect(groupingBy(e -> {
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(e));
                return jsonObject.getString(groupA);
            }, Collectors.groupingBy(e -> {
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(e));
                return jsonObject.getString(groupB);
            })));
            return collect;
        }
        return null;
    }
}

你可能感兴趣的:(java,lamda,多级分组,对象分组,json分组)