List 集合相关方法 Sucha

Administrator
发布于 2020-07-22 / 1484 阅读 / 0 评论 / 0 点赞

List 集合相关方法 Sucha

List 集合相关方法 Sucha

1、将集合List中的对象,根据对象的id进行数据合并

问题描述:

比如业务员每天销售额归总

再比如我查询后台的相关数据,需要区分信息,字段内相同的id下,可能有用户1类型和用户2类型,且需要将用户1的购买份数与之对应的单价相乘,将用户2的购买份数与之对应的单价相乘,最终将两个金额相加,得出一个,剔除另一个。

思想:就是循环list集合,然后再判断在map集合里有没有list循环出的id生成的key,如果没有相应的key,就把该循环下的实体类放到map里,然后进入下一次循环,直至遇到和map里相同的key,就不放到map里,且将已经有的key和此时循环出的实体类的内容相加或相乘等操作。

(方法1)java8的Lombad表达式

  • 创建实体类
public class People {
    private int id;//id编号
    private String name;//姓名
    private double inCome;//收入
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public double getInCome() {
        return inCome;
    }
 
    public void setInCome(double inCome) {
        this.inCome = inCome;
    }
 
    @Override
    public String toString() {
        return "People{" +"id=" + id +", name='"
                + name + '\'' +", inCome=" + inCome +'}';
    }
}
  • 初始化数据,并将多条数据进行合并


public class TestListChange {
    public static void main(String args[]) {
        //为集合初始化多条数据,根据id 合并inCome字段使用
        People p1 = new People();
        p1.setId(1001);
        p1.setName("张三");
        p1.setInCome(5000.0);
 
        People p2 = new People();
        p2.setId(1002);
        p2.setName("李四");
        p2.setInCome(8000.0);
 
        People p3 = new People();
        p3.setId(1003);
        p3.setName("王二");
        p3.setInCome(5350.0);
 
        People p4 = new People();
        p4.setId(1003);
        p4.setName("王二");
        p4.setInCome(3000);
 
        People p5 = new People();
        p5.setId(1002);
        p5.setName("李四");
        p5.setInCome(4500);
 
        List<People> list = new ArrayList();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        
        //遍历集合中数据
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }
 
        System.out.println("================================================");
 
        List<People> changeList = getNewList(list);
        //合并后的数据条数
        System.out.println("合并后的数据条数:"+changeList.size());
        //遍历合并后的数据
        for (People people : changeList) {
            if(people!=null)
            System.out.println(people.toString());
        }
    }
    
}
  • 核心部分为静态 方法 getNewList()部分
private static List<People> getNewList(List<People> list) {
       List<People> list = Lists.newArrayList();
        Map<Integer, List<People>> peopleGroupById = list.stream().collect(Collectors.groupingBy(People::getId));
        List<People> newList = Lists.newArrayList();
        for (Integer peopleId : peopleGroupById.keySet()) {
            List<People> peopleList = peopleGroupById.get(peopleId);
            double totalInCome = peopleList.stream().map(People::getInCome).reduce(0.0, Double::sum);
            peopleList.get(0).setInCome(totalInCome);
            newList.add(peopleList.get(0));
        }
 		return newList;
    }
  • 运行效果图

img

(方法2)普通方式

  • 仅对getNewList方法做了更改
private static Collection<MealReduceReportExt> getNewList(List<MealReduceReportExt> list) {
    Map<String, People2> results = new HashMap<>();
            for (People2 people : list) {
                if ("staff".equals(people.getBuyerType())) {
                    BigDecimal result = people.getStaffPrice().multiply(BigDecimal.valueOf(people.getMealCount()));
                    people.setSumMoney(result);
                } else {
                    BigDecimal result = people.getPatientPrice().multiply(BigDecimal.valueOf(people.getMealCount()));
                    people.setSumMoney(result);
                }

                if (results.get(people.getMealId()) == null) {
                    results.put(people.getMealId(), people);
                } else {
                    People2 people1 = results.get(people.getMealId());
                    BigDecimal sum = people1.getSumMoney().add(people.getSumMoney());
                     mealReduceReportExt1.setMealCount(mealCount);
                     mealReduceReportExt1.setSumMoney(sum);
                     mealReduceReportExt1.setBuyerType("all");
                }
            }

            Collection<People2> values = results.values();
            return values;
}

2、List集合合并-相同的实体类效果最好,或者说只能相同的实体类

public static void main(String[] args) {
       //查数据
		List<BalanceReportExt> list = balanceReportDao.search(balanceReport);

		//单独查补卡的记录
		List<BalanceReportExt> replaceList = balanceReportDao.searchReplace(balanceReport);
		//合并两个list
		List<BalanceReportExt> allLists = new ArrayList<>();
		allLists.addAll(list);
		allLists.addAll(replaceList);
        
    }
  • 最后 如果两个时间相同还要根据 具体内容排序 可以count= 0时继续排序

BalanceReportExt对象:


import java.io.Serializable;


public class BalanceReportExt implements Serializable {
 
    private String consumerNo;
	
	private String consumerName;
	
	private String consumerType;
	
	private String cardNo;
	
	private String paymentType;

	private String action;
	
	private Date actionDate;
	
	@JSONField(serialize = false)
	private BigDecimal actionMoney;
	
	private BigDecimal cardMoney;

	private BigDecimal remainMoney;

	private BigDecimal clearCardMoney;

	private BigDecimal replaceCardMoney;
}

2.1、并按照某个字段排序

if (allLists.size()>0&&allLists!=null){
    //根据actiondate排序集合
    Collections.sort(allLists, new Comparator<BalanceReportExt>() {
        @Override
        public int compare(BalanceReportExt o1, BalanceReportExt o2) {
            //比较时间毫秒数大小
            int i = o1.getActionDate().compareTo(o2.getActionDate());
            if (i == 0){
                return 0;
            }
            if (i > 0){
                return -1;
            }
            return  1 ;
        }
    });
}

评论