Java 8添加了对象列表的多个属性的值 [英] Java 8 adding values of multiple property of an Object List

查看:934
本文介绍了Java 8添加了对象列表的多个属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说下面有一个类有getter和setter但只有默认构造函数。

Lets say I have a class below with getters and setters but with only default constructor.

注意:我不允许更改此课程的结构。

Note: I'm not allowed to change the structure of this class.

class Target {

    private String year;
    private String month;
    private String name;
    private double target;
    private double achieved;


    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getTarget() {
        return target;
    }

    public void setTarget(double target) {
        this.target = target;
    }

    public double getAchieved() {
        return achieved;
    }

    public void setAchieved(double achieved) {
        this.achieved = achieved;
    }    
}

我必须添加Target和Achieved列值在一年和名字。

I have to add the Target and Achieved columns values based on year and Name.

Year Month Name Target Achieved
2018  8    Joey 50.00   10.00
2018  9    Joey 200.00  100.00
2018  9    Fred 200.00  150.00
2018  9    Fred 20.00   50.00

因此输出将是:

Year Month Name Target  Achieved
2018  8    Joey 50.00   10.00
2018  9    Joey 200.00  100.00
2018  9    Fred 220.00  200.00

我见过如何实现这样的事情的例子我有一个接受参数的构造函数,但我对概念不太清楚分组和汇总对象,如SQL中的Java lambdas?

I've seen an example on how I could achieve something like this if I had a constructor that accepts parameters but I'm not so clear on the concept Group by and sum objects like in SQL with Java lambdas?:

我如何实现这一点获取相同类型的默认构造函数列表< Target> 但是计算了多列的值?

How do I achieve this with just the default constructor to get same type List<Target> but with calculated values of multiple columns?

推荐答案

您似乎需要根据以下三件事进行分组:名称,所以这可能如下所示:

It seems that you need to group based on three things: Year, Month and Name, so this could look like this:

Collection<Target> merged = yourListOfTargets
            .stream()
            .collect(Collectors.toMap(
                    t -> List.of(t.getYear(), t.getMonth(), t.getName()),
                    Function.identity(),
                    (left, right) -> {
                        left.setTarget(left.getTarget() + right.getTarget());
                        left.setAchieved(left.getAchieved() + right.getAchieved());
                        return left;
                    }))
            .values();

正如Federico在评论中提到的,这将改变您在初始列表中的元素。您可能没问题,但如果不是,则需要使用复制函数替换 Function.identity(),这将创建一个新的从现有目标

As Federico mentions in comments, this will alter your elements in the initial List. You might be OK with it, but if you are not, you need to replace Function.identity() with a copying Function that would create a new Target from an existing one.

这篇关于Java 8添加了对象列表的多个属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆