Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Find the sum of values of an Employee and its Parent using stream

Consider, I have the following class.

public class Employee {

private Integer id;
private Integer parentId;
private int value;
}

Now I have the following records.

    Employee employee1 = new Employee(1, null, 4);
    Employee employee2 = new Employee(2, 1, 4);
    Employee employee3 = new Employee(3, null, 8);
    Employee employee4 = new Employee(4, 1, 3);
    Employee employee5 = new Employee(5, 2, 11);

So the parentId is the id of an employee who is the boss of another employee.

The question is to find out the sum of the value of an employee and it's parent if it has any, or return the value as it is. So in this case, the result should be.

id value
1 4
2 8
3 8
4 7
5 15

How to achieve this using java 8 streams? I have tried the following;

  Optional<Integer> first = employees.stream().map(a -> {
                int sum = 0;
                employees.stream().map(b -> {
                    if (Objects.equals(a.id, b.parentId)) {
                        sum = sum + a.value + b.value;
                    }else{
                        sum  = sum + a.value;
                    }
                    return sum;
                });
                return sum;
            }
    ).findFirst();

But I am getting error with the sum variable that it should be final or effectively final.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

By computing only the value of the child and its first parent, you can do it with a simple filter on the list.

@Test
public void test() {
    final Employee employee1 = new Employee(1, null, 4);
    final Employee employee2 = new Employee(2, 1, 4);
    final Employee employee3 = new Employee(3, null, 8);
    final Employee employee4 = new Employee(4, 1, 3);
    final Employee employee5 = new Employee(5, 2, 11);

    final List<Employee> employees = List.of(employee1, employee2, employee3, employee4, employee5);
    final List<Pair<Integer, Integer>> res = employees
            .stream()
            .map(emp -> Pair.of(emp.id,
                            emp.value + employees
                                    .stream()
                                    .filter(e -> e.id.intValue() == Optional.ofNullable(emp.parentId).orElse(-1))
                                    .findFirst().map(Employee::getValue)
                                    .orElse(0)
                    )
            )
            .collect(Collectors.toList());
    res.forEach(System.out::println);
}

public class Employee {
    public Integer id;
    public Integer parentId;
    public int value;
    public Employee(final Integer id, final Integer parentId, final int value) {
        this.id = id;
        this.parentId = parentId;
        this.value = value;
    }
    public int getValue() {
        return this.value;
    }
}

result :

(1,4)
(2,8)
(3,8)
(4,7)
(5,15)

NB: the object Pair I used comes from org.apache.commons.lang3.tuple.Pair

over 4 years ago · Santiago Trujillo Relatório

0

The most efficient way to achieve that is to create a map which will allow to retrieve the value of an Employee by its id.

Then, in order to combine the value of each Employee with its parent's value, use the value from the map or 0 if the parent is null.

Method below generates a map with id used as a key and employee's value as a value.

public static Map<Integer, Integer> getEmplValueById(List<Employee> employees) {
    return employees.stream()
            .collect(Collectors.toMap(Employee::getId,
                                      Employee::getValue));
}

The following method creates a map with id used as a key and the total value of an employee as a map's value.

public static Map<Integer, Integer> getEmplSumById(List<Employee> employees,
                                                   Map<Integer, Integer> valueById) {
    return employees.stream()
            .collect(Collectors.toMap(Employee::getId,
                        empl -> empl.getValue() + 
                                valueById.getOrDefault(empl.getParentId(), 0)));
}

main()

public static void main(String[] args) {
    List<Employee> employees = List.of(
            new Employee(1, null, 4),
            new Employee(2, 1, 4),
            new Employee(3, null, 8),
            new Employee(4, 1, 3),
            new Employee(5, 2, 11));

    Map<Integer, Integer> valueById = getEmplValueById(employees);

    getEmplSumById(employees, valueById)
         .forEach((k, v) -> System.out.printf("ID %d \t value %d\n", k, v));
}

Output

ID 1     value 4
ID 2     value 8
ID 3     value 8
ID 4     value 7
ID 5     value 15
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda