Java lambda表达式集合过滤--两个List中Map的某个属性相除

说明:Java 两个 List中Map的id属性相同时,将两个map中的value属性相除。
List中嵌套Map,Map当中的属性有id,name,value。

List中为对象:

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args){
        List<Energy> beforeList = new ArrayList<>();
        beforeList.add(new Energy(1, "张三去年家用电", 183));
        beforeList.add(new Energy(2, "李四去年家用电", 276));
        beforeList.add(new Energy(3, "王五去年家用电", 105));

        List<Energy> thisList = new ArrayList<>();
        thisList.add(new Energy(1, "张三今年家用电", 241));
        thisList.add(new Energy(2, "李四今年家用电", 205));
        thisList.add(new Energy(3, "王五今年家用电", 87));

        for (Energy energy : thisList) {
            if (energy!=null) {
                Double dr = Double.valueOf(0);
                Double thisValue = Double.valueOf(energy.getValue());
                List<Energy> list= beforeList.stream()
                        .filter(objectMap -> objectMap.getId().equals(energy.getId()))
                        .collect(Collectors.toList());
                if(list!=null && list.size()>0){
                    if(list.get(0)!=null && list.get(0).getId()!=null && Double.valueOf(String.valueOf(list.get(0).getValue()))!=0){
                        Double beforeValue = Double.valueOf(String.valueOf(list.get(0).getValue()));
                        dr = (beforeValue- thisValue)/beforeValue;
                    }
                }
                Double conservationRate =  BigDecimal.valueOf(dr).multiply(new BigDecimal(100))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                System.out.println("节能率=(去年-今年)/去年*100%:" + conservationRate);
            }
        }
    }
}

class Energy{
    //id
    private Integer id;
    //名称
    private String name;
    private Integer value;
    public Energy(){

    }
    public Energy(Integer id, String name, Integer value) {
        this.id = id;
        this.name = name;
        this.value = value;
    }

    public void setId(Integer id){
        this.id = id;
    }
    public Integer getId(){
        return id;
    }
    public void setName(String name){
        this.name= name;
    }
    public String getName(){
        return name;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

List中为Map:

for (Map<String, Object> map : beforeList) {
    if (!map.isEmpty()) {
        Double dr = Double.valueOf(0);
        Double thisValue = Double.valueOf(String.valueOf(map.get("value")));
        List<Map<String, Object>> list= thisList.stream()
                .filter(objectMap -> Integer.parseInt(String.valueOf(objectMap.get("id"))) == Integer.parseInt(String.valueOf(map.get("id"))))
                .collect(Collectors.toList());
        if(list!=null && list.size()>0){
            if(list.get(0)!=null && list.get(0).get("id")!=null && Double.valueOf(String.valueOf(list.get(0).get("value")))!=0){
                Double lastValue = Double.valueOf(String.valueOf(list.get(0).get("value")));
                dr = (lastValue- thisValue)/lastValue;
            }
        }
        Double rate =  BigDecimal.valueOf(dr).multiply(new BigDecimal(100))
                .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        System.out.println("节能率=(去年-今年)/去年*100%:" + rate);
        map.put("rate",rate);
    }
}

欢迎大神指正,欢迎更优写法!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值