Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.function.BinaryOperator;
  3. import java.util.function.Function;
  4. import java.util.stream.Collectors;
  5.  
  6. public class TestTry {
  7.  
  8.     public static void main(String[] args) {
  9.         List<TempStat> stats = new ArrayList<TempStat>();
  10.         stats.add(new TempStat("Minsk", 18, new Date(2020, 1, 20)));
  11.         stats.add(new TempStat("Minsk", 19, new Date(2020, 1, 21)));
  12.         stats.add(new TempStat("Minsk", 20, new Date(2020, 1, 22)));
  13.         stats.add(new TempStat("New York", 8, new Date(2020, 1, 20)));
  14.         stats.add(new TempStat("New York", 9, new Date(2020, 1, 21)));
  15.         stats.add(new TempStat("New York", 10, new Date(2020, 1, 22)));
  16.         stats.add(new TempStat("London", 20, new Date(2020, 1, 20)));
  17.         stats.add(new TempStat("London", 19, new Date(2020, 1, 21)));
  18.         stats.add(new TempStat("London", 18, new Date(2020, 1, 22)));
  19.  
  20.         Collection<TempStat> result = stats.stream().collect(Collectors.toMap(
  21.                 TempStat::getCity,
  22.                 Function.identity(),
  23.                 BinaryOperator.maxBy(Comparator.comparing(TempStat::getDate))))
  24.                 .values();
  25.  
  26.         result.stream()
  27.                 .forEach(System.out::println);
  28.     }
  29.  
  30.  
  31.     static class TempStat {
  32.         String city;
  33.         int value;
  34.         Date date;
  35.  
  36.         public TempStat(String city, int value, Date date) {
  37.             this.city = city;
  38.             this.value = value;
  39.             this.date = date;
  40.         }
  41.  
  42.         public String getCity() {
  43.             return city;
  44.         }
  45.  
  46.         public Date getDate() {
  47.             return date;
  48.         }
  49.  
  50.         @Override
  51.         public String toString() {
  52.             return "TempStat{" +
  53.                     "city='" + city + '\'' +
  54.                     ", value=" + value +
  55.                     ", date=" + date +
  56.                     '}';
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement