Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!