Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda