Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<String> getMostValuableItemNames(List<UserWithItems> users) {
- return users.stream()
- .flatMap(u -> u.getItems().stream())
- .sorted()
- .distinct()
- .map(Item::getName)
- .collect(toList());
- }
- @Data
- static class UserWithItems {
- private final List<Item> items;
- }
- @Data
- static class Item implements Comparable<Item> {
- private final int value;
- private final String name;
- @Override
- public int compareTo(Item o) {
- return Integer.compare(value, o.value);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Item item = (Item) o;
- return value == item.value &&
- Objects.equals(name, item.name);
- }
- @Override
- public int hashCode() {
- return Objects.hash(value, name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement