Oracle
1Z0-819 · Question #100
A bookstore's sales are represented by a list of Sale objects populated with the name of the customer and the books they purchased. public class Sale { String customer; private List<Book> items; /…
The correct answer is C. lList.stream() .collect(groupingBy(Sale::getCustomer, summingDouble(sale -> sale.getItems().stream().mapToDouble(Book::getPrice).sum()))); .entrySet().stream().sorted(Map.Entry.comparingByValue()) .collect(mapping(Entry::getKey, toList())). See the full explanation below for the reasoning.
Question
A bookstore's sales are represented by a list of Sale objects populated with the name of the customer and the books they purchased.
public class Sale {
String customer;
private List<Book> items;
/* constructor, setters and getters not shown */
}
public class Book {
private String name;
private double price;
/* constructor, setters and getters not shown */
}
Given a list of Sale objects, lList, which code fragment creates a list of total sales for each customer in ascending order?
Options
- AlList.stream() .collect(groupingBy(Sale::getCustomer, TreeMap::new, summingDouble(sale -> sale.getItems().stream().mapToDouble(Book::getPrice).sum()))); .entrySet().stream().sorted(Map.Entry.comparingByValue()) .collect(mapping(Entry::getKey, toList()));
- BlList.stream() .collect(groupingBy(Sale::getCustomer, summingDouble(sale -> sale.getItems().stream().mapToDouble(Book::getPrice).sum()))); .entrySet().stream().sorted(Map.Entry.comparingByValue()) .collect(mapping(Entry::getKey, toList()));
- ClList.stream() .collect(groupingBy(Sale::getCustomer, summingDouble(sale -> sale.getItems().stream().mapToDouble(Book::getPrice).sum()))); .entrySet().stream().sorted(Map.Entry.comparingByValue()) .collect(mapping(Entry::getKey, toList()));
- DlList.stream() .collect(groupingBy(Sale::getCustomer, summingDouble(sale -> sale.getItems().stream().mapToDouble(Book::getPrice).sum()))); .entrySet().stream().sorted(Map.Entry.comparingByValue()) .collect(mapping(Entry::getKey, toList()));
How the community answered
(14 responses)- B7% (1)
- C79% (11)
- D14% (2)
Community Discussion
No community discussion yet for this question.