Remove a list of user-defined class object and combine value of one property in Java

huangapple go评论63阅读模式
英文:

Remove a list of user-defined class object and combine value of one property in Java

问题

我有一个Employee


    private final String id;
    private float compensation;

    public Employee(String id, float compensation) {
        this.id = id;
        this.compensation = compensation;
    }

    public String getId() {
        return id;
    }
    
    public float getCompensation() {
        return compensation;
    }
}

然后我有一个员工列表

      new Employee("1", 3000.00),
      new Employee("1", 6000.00),
      new Employee("2", 5000.00),
      new Employee("3", 4000.00),
);

如何通过员工ID去重列表并合并补偿?

期望结果:

      new Employee("1", 9000.00),
      new Employee("2", 5000.00),
      new Employee("3", 4000.00),
);
英文:

I have a Employee class

class Employee {

    private final String id;
    private float compensation;

    public Employee(String id, float compensation) {
        this.id = id;
        this.compensation = compensation;
    }

    public String getId() {
        return id;
    }
    
    public float getCompensation() {
        return compensation;
    }
}

Then I have a list of Employees

List<Employee> payment = Arrays.asList(
      new Employee("1", 3000.00),
      new Employee("1", 6000.00),
      new Employee("2", 5000.00),
      new Employee("3", 4000.00),
);

How to dedup the list by the employee ID and combine the compensation?

Expected result:

List<Employee> newPayment = Arrays.asList(
      new Employee("1", 9000.00),
      new Employee("2", 5000.00),
      new Employee("3", 4000.00),
);

答案1

得分: 3

你可以使用Java 8的流(streams)和收集器(collectors)来根据他们的 id 对员工进行分组,然后对他们的薪酬进行求和。以下是一个示例代码片段:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


class Dedup {
    public static List<Employee> dedupByIds(List<Employee> payment) {
        return payment.stream()
                .collect(Collectors.groupingBy(Employee::getId, Collectors.summingDouble(Employee::getCompensation)))
                .entrySet().stream()
                .map(entry -> new Employee(entry.getKey(), entry.getValue().floatValue()))
                .collect(Collectors.toList());
    }
}

然后通过要去重的列表调用这个函数。

List<Employee> payment = Arrays.asList(
                new Employee("1", 3000.00f),
                new Employee("1", 6000.00f),
                new Employee("2", 5000.00f),
                new Employee("3", 4000.00f)
                );

List<Employee> newPayment = Dedup.dedupByIds(payment);

//将值打印到控制台
for (Employee e : newPayment) {
    System.out.println(e.getId() + " : " + e.getCompensation());
}
英文:

You can use Java 8 streams and collectors to group the employees by their id and then sum their compensations. Here's an example code snippet:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


class Dedup {
    public static List<Employee> dedupByIds(List<Employee> payment) {
        return payment.stream()
                .collect(Collectors.groupingBy(Employee::getId, Collectors.summingDouble(Employee::getCompensation)))
                .entrySet().stream()
                .map(entry -> new Employee(entry.getKey(), entry.getValue().floatValue()))
                .collect(Collectors.toList());
    }
}

and then call this function by the list that you want to dedup.

List<Employee> payment = Arrays.asList(
                new Employee("1", 3000.00f),
                new Employee("1", 6000.00f),
                new Employee("2", 5000.00f),
                new Employee("3", 4000.00f)
                );

List<Employee> newPayment = Dedup.dedupByIds(payment);

//printing values to console 
for (Employee e : newPayment) {
    System.out.println(e.getId() + " : " + e.getCompensation());
}

答案2

得分: 1

为了通过员工ID去重并合并薪酬,您可以使用Java流和分组操作。以下是一个示例:

List<Employee> newPayment = payment.stream()
        .collect(Collectors.groupingBy(Employee::getId,
                 Collectors.summingDouble(Employee::getCompensation)))
        .entrySet().stream()
        .map(entry -> new Employee(entry.getKey(), (float) entry.getValue()))
        .collect(Collectors.toList());

在这段代码中,我们首先使用groupingBy()收集器根据员工的id属性对Employee对象进行分组。我们还使用summingDouble()收集器来计算每个组的薪酬总和。

这创建了一个Map<String, Double>,其中键是员工ID,值是每个组的总薪酬。

接下来,我们使用map()方法将映射条目转换回Employee对象,最后使用toList()收集器将它们收集到一个新的列表中。结果是一个包含合并薪酬的新的Employee对象列表。

英文:

To dedup the list by employee ID and combine the compensation, you can use Java streams and grouping operations. Here's an example:

List<Employee> newPayment = payment.stream()
        .collect(Collectors.groupingBy(Employee::getId,
                 Collectors.summingDouble(Employee::getCompensation)))
        .entrySet().stream()
        .map(entry -> new Employee(entry.getKey(), (float) entry.getValue()))
        .collect(Collectors.toList());

In this code, we first use the groupingBy() collector to group the Employee objects by their id property. We also use the summingDouble() collector to calculate the sum of compensation for each group.

This creates a Map<String, Double> where the keys are the employee IDs and the values are the total compensations for each group.

Next, we convert the map entries back to Employee objects using the map() method, and finally collect them into a new list using the toList() collector. The result is a new list of Employee objects with the combined compensations.

huangapple
  • 本文由 发表于 2023年4月13日 15:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定