Advertisement
aloginovpro

Untitled

Oct 12th, 2020
1,878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1.    
  2.     public List<String> getMostValuableItemNames(List<UserWithItems> users) {
  3.         return users.stream()
  4.                 .flatMap(u -> u.getItems().stream())
  5.                 .sorted()
  6.                 .distinct()
  7.                 .map(Item::getName)
  8.                 .collect(toList());
  9.     }
  10.    
  11.     @Data
  12.     static class UserWithItems {
  13.         private final List<Item> items;
  14.     }
  15.    
  16.     @Data
  17.     static class Item implements Comparable<Item> {
  18.        
  19.         private final int value;
  20.         private final String name;
  21.  
  22.         @Override
  23.         public int compareTo(Item o) {
  24.             return Integer.compare(value, o.value);
  25.         }
  26.  
  27.         @Override
  28.         public boolean equals(Object o) {
  29.             if (this == o) return true;
  30.             if (o == null || getClass() != o.getClass()) return false;
  31.             Item item = (Item) o;
  32.             return value == item.value &&
  33.                     Objects.equals(name, item.name);
  34.         }
  35.  
  36.         @Override
  37.         public int hashCode() {
  38.             return Objects.hash(value, name);
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement