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

209
Visualizações
Fail to write the reduce method using Stream API
@Getter
public class Dish {
   BigDecimal price;
}

I need to calculate the total price of all ordered dishes, but I fail to write the reduce method. This is a method signature ( argument has a map of Dish and how many times it was ordered ).

So it must be something like this sum of every dish.getPrice * dishQuantaty

    private BigDecimal getOrderTotalPrice(Map<Dish, Integer> dishQuantityMap) {
}

The fail-code I was asked about

  return   dishQuantityMap.entrySet().stream()
        .reduce(BigDecimal.ZERO,
                (dishIntegerEntry) ->
               dishIntegerEntry.getKey().getPrice()
                        .multiply(BigDecimal.valueOf(dishIntegerEntry.getValue())));
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Did you mean something like this:

private BigDecimal getOrderTotalPrice(Map<Dish, Integer> dishQuantityMap) {
  return dishQuantityMap.entrySet().stream()
          .map(d -> d.getKey().getPrice().multiply(new BigDecimal(d.getValue())))
          .reduce(BigDecimal.ZERO, BigDecimal::add);
}
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