Java相同id的数据集合,合并数据为一条,并将几个字段内容合并为一个

Java实现,当然也可以数据库实现;

/**
 * Created by shaozhiqi on 2019/7/31.
 */
public class TestUnion {

    @Test
    public void test1() {
        List> dbList = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("id", "1");
        map1.put("assort_no", "11111");
        map1.put("name", "test");
        map1.put("leve1", "high");
        map1.put("leve1Desc", "高风险");
        dbList.add(map1);

        Map map2 = new HashMap<>();
        map2.put("id", "2");
        map2.put("assort_no", "11111");
        map2.put("name", "test");
        map2.put("leve1", "low");
        map2.put("leve1Desc", "低风险1");
        dbList.add(map2);


        Map map3 = new HashMap<>();
        map3.put("id", "3");
        map3.put("assort_no", "11222");
        map3.put("name", "test2");
        map3.put("leve1", "low");
        map3.put("leve1Desc", "低风险2");
        dbList.add(map3);

        Map map4 = new HashMap<>();
        map4.put("id", "4");
        map4.put("assort_no", "11111");
        map4.put("name", "test");
        map4.put("leve1", "low");
        map4.put("leve1Desc", "低风险3");
        dbList.add(map4);

        Map map5 = new HashMap<>();
        map5.put("id", "5");
        map5.put("assort_no", "11111");
        map5.put("name", "test");
        map5.put("leve1", "high");
        map5.put("leve1Desc", "高风险5");
        dbList.add(map5);

        for (Map m : dbList) {
            List high = new ArrayList();
            List low = new ArrayList();
            for (Map mm : dbList) {
                if (mm.get("assort_no").toString().equals(m.get("assort_no").toString())) {
                    if ("high".equals(mm.get("leve1").toString())) {
                        high.add(mm.get("leve1Desc").toString());
                    } else if ("low".equals(mm.get("leve1").toString())) {
                        low.add(mm.get("leve1Desc").toString());
                    }
                }
            }
            m.put("high", high);
            m.put("low", low);
        }

        dbList.forEach(m -> {
            System.out.println(GSON.toJson(m));
        });

        Map mapResult=new HashMap<>();
        for (Map m : dbList) {
            mapResult.put(m.get("assort_no").toString(),m);
        }
       System.out.println("========================去重后==========================");
        mapResult.forEach((k,v) -> {
            System.out.println(GSON.toJson(v));
        });
    }


}

结果:

{"high":["高风险","高风险5"],"low":["低风险1","低风险3"],"assort_no":"11111","name":"test","leve1":"high","id":"1","leve1Desc":"高风险"}
{"high":["高风险","高风险5"],"low":["低风险1","低风险3"],"assort_no":"11111","name":"test","leve1":"low","id":"2","leve1Desc":"低风险1"}
{"high":[],"low":["低风险2"],"assort_no":"11222","name":"test2","leve1":"low","id":"3","leve1Desc":"低风险2"}
{"high":["高风险","高风险5"],"low":["低风险1","低风险3"],"assort_no":"11111","name":"test","leve1":"low","id":"4","leve1Desc":"低风险3"}
{"high":["高风险","高风险5"],"low":["低风险1","低风险3"],"assort_no":"11111","name":"test","leve1":"high","id":"5","leve1Desc":"高风险5"}
======================去重后============================
{"high":["高风险","高风险5"],"low":["低风险1","低风险3"],"assort_no":"11111","name":"test","leve1":"high","id":"5","leve1Desc":"高风险5"}
{"high":[],"low":["低风险2"],"assort_no":"11222","name":"test2","leve1":"low",

 

转载于:https://www.cnblogs.com/shaozhiqi/p/11535221.html

你可能感兴趣的:(Java相同id的数据集合,合并数据为一条,并将几个字段内容合并为一个)