用Map中一个Value将List<Map>进行分组成List<对象>(根据对象的属性去重)或者List<Map>(List<Map>根据Value去重)

可以用stream流进行处理,下面这个自己写的有用就参考一下

一、分组成List<对象>

先建一个实体类作为分组后的每一个对象

package com.njusc.core.api.dto;

import java.util.List;
import java.util.Map;

/**
 *
 */
public class PingzDc {
    private String pcId;
    private List list;

    @Override
    public String toString() {
        return "PingzDc{" +
                "pcId='" + pcId + '\'' +
                ", list=" + list +
                '}';
    }

    public String getPcId() {
        return pcId;
    }

    public void setPcId(String pcId) {
        this.pcId = pcId;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}

然后进行分组(我是根据组织名称进行分组的deptName)

            List list = initList(pingzList, user);//传过来的数据
            List mapList = list;//复制一份List
            List pingzDcList = new ArrayList<>();//创建一个上面的对象集合
            for (Map map : list) {
                String pcId= (String) map.get("pcId");//获取Value
                PingzDc pingzDc = new PingzDc();//创建对象
                pingzDc.setPcId(pcId);//将Value值赋值进去
                List list1 = new ArrayList<>();
                for (Map map1 : mapList) {
                   String pcId1= (String) map1.get("pcId");
                   if (pcId.equals(pcId1)){//从复制的List中将Value取出来进行比较
                        list1.add(map1);
                   }
                }
                pingzDc.setList(list1);
                pingzDcList.add(pingDc);
            }
            //最后根据Value进行一个去重
            Set set 
            = new TreeSet<>(Comparator.comparing(PingzDc::getPcId));
            set.addAll(pingzDcList);
            pingzDcList =new ArrayList<>(set);

 结果:

[PingzDc {
  pcId = '1', 
  list = [{
      buildFrom = 11,
      zhaiy = 村集体组织zcy001,
      money_j = 22.00,
      onlyKmName = 银行存款,
      jzState = 0,
      pzCx = null,
      money_d = ,
      userName = 张女士,
      pzCxId = null,
      pzDate = 2023 - 01 - 31,
      pzId = 1
 }, {
      buildFrom = 11,
      zhaiy = 村集体组织zcy001,
      money_j = ,
      onlyKmName = 银行存款,
      jzState = 0,
      pzCx = null,
      money_d = 11.00,
      userName = 张女士,
      pzCxId = null,
      pzDate = 2023 - 01 - 31,
      pzId = 1
 }]
}, 
PingzDc {
 pcId = '2', 
 list = [{
      buildFrom = 11,
      zhaiy = 收入zcy001,
      money_j = 100.00,
      onlyKmName = 现金,
      jzState = 0,
      pzCx = null,
      money_d = ,
      userName = 张女士,
      pzCxId = null,
      pzDate = 2023 - 01 - 31,
      pzId = 2
 }, {
      buildFrom = 11,
      zhaiy = 收入zcy001,
      money_j = ,
      onlyKmName = 现金,
      jzState = 0,
      pzCx = null,
      money_d = 100.00,
      userName = 张女士,
      pzCxId = null,
      pzDate = 2023 - 01 - 31,
      pzId = 2
 }]
}, 
PingzDc {
 pcId = '3', 
 list = [{
      buildFrom = 11,
      zhaiy = 支出倒叙,
      money_j = 100.00,
      onlyKmName = 银行存款,
      jzState = 0,
      pzCx = null,
      money_d = ,
      use
......

二、分组成List

                List list1 = initList(pingzList, user);//这个是你的List
                List mapList1 = list1;
                List pingzDcList = new ArrayList<>();
                for (Map map : list1) {
                    String pzId = (String) map.get("pzId");
                    Map map3 = new HashMap();
                    map3.put("pzId",pzId);
                    List list2 = new ArrayList<>();
                    for (Map map1 : mapList1) {
                        String pzId1 = (String) map1.get("pzId");
                        if (pzId.equals(pzId1)){
                            list2.add(map1);
                        }
                    }
                    map3.put("list",list2);
                    pingzDcList.add(map3);
                }
                //去重此时List是你要的List(List根据Map中的Value去重)
                List list = pingzDcList.stream().collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                     () ->new TreeSet<>(Comparator.comparing(m- >m.get("pzId").toString()))
                                ),ArrayList::new
                        )
                );

结果:

[{
	"pzId": "1",
	"list": [{
		"buildFrom": 11,
		"zhaiy": "村集体组织zcy001",
		"money_j": "22.00",
		"onlyKmName": "银行存款",
		"jzState": 0,
		"money_d": "",
		"userName": "张女士",
		"pzDate": "2023-01-31",
		"pzId": "1"
	}, {
		"buildFrom": 11,
		"zhaiy": "村集体组织zcy001",
		"money_j": "",
		"onlyKmName": "银行存款",
		"jzState": 0,
		"money_d": "11.00",
		"userName": "张女士",
		"pzDate": "2023-01-31",
		"pzId": "1"
	}]
}, {
	"pzId": "2",
	"list": [{
		"buildFrom": 11,
		"zhaiy": "收入zcy001",
		"money_j": "100.00",
		"onlyKmName": "现金",
		"jzState": 0,
		"money_d": "",
		"userName": "张女士",
		"pzDate": "2023-01-31",
		"pzId": "2"
	}, {
		"buildFrom": 11,
		"zhaiy": "收入zcy001",
		"money_j": "",
		"onlyKmName": "现金",
		"jzState": 0,
		"money_d": "100.00",
		"userName": "张女士",
		"pzDate": "2023-01-31",
		"pzId": "2"
	}]
}, {
	"pzId": "3",
	"list": [{
		"buildFrom": 11,
		"zhaiy": "支出倒叙",
		"money_j": "100.00",
		"onlyKmName": "银行存款",
		"jzState": 0,
		"money_d": "",
		"userName": "张女士",
		"pzDate": "2023-01-31",
		"pzId": "3"
        .......

你可能感兴趣的:(java,java)