fork download
  1. import java.util.*;
  2. class WeatherRecord {
  3. String year;
  4. float temperature;
  5.  
  6. WeatherRecord(String year, float temperature) {
  7. this.year = year;
  8. this.temperature = temperature;
  9. }
  10. }
  11.  
  12. class Mapper {
  13. List<WeatherRecord> map(String[] lines) {
  14. List<WeatherRecord> mappedData = new ArrayList<>();
  15. for (String line : lines) {
  16. if (!line.startsWith("Date")) {
  17. String[] parts = line.split(",");
  18. String year = parts[4];
  19. float temp = Float.parseFloat(parts[1]);
  20. mappedData.add(new WeatherRecord(year, temp));
  21. }
  22. }
  23. return mappedData;
  24. }
  25. }
  26.  
  27. class Reducer {
  28. Map<String, Float> reduce(List<WeatherRecord> mappedData) {
  29. Map<String, Float> maxTemps = new HashMap<>();
  30. for (WeatherRecord record : mappedData) {
  31. maxTemps.put(record.year, Math.max(maxTemps.getOrDefault(record.year, Float.MIN_VALUE), record.temperature));
  32. }
  33. return maxTemps;
  34. }
  35. }
  36.  
  37. public class Main {
  38. public static void main(String[] args) {
  39. String[] inputData = {
  40. "Date,Temperature,Humidity,Wind,Year",
  41. "2021-04-10,35.2,45,12,2021",
  42. "2021-04-11,36.1,50,10,2021",
  43. "2022-04-12,37.8,48,11,2022",
  44. "2022-05-01,38.3,52,10,2022",
  45. "2023-04-10,33.2,47,13,2023"
  46. };
  47.  
  48. Mapper mapper = new Mapper();
  49. List<WeatherRecord> mapped = mapper.map(inputData);
  50.  
  51. Reducer reducer = new Reducer();
  52. Map<String, Float> results = reducer.reduce(mapped);
  53.  
  54. for (Map.Entry<String, Float> entry : results.entrySet()) {
  55. System.out.println("Year: " + entry.getKey() + ", Max Temp: " + entry.getValue());
  56. }
  57. }
  58. }
Success #stdin #stdout 0.15s 55776KB
stdin
Standard input is empty
stdout
Year: 2023, Max Temp: 33.2
Year: 2022, Max Temp: 38.3
Year: 2021, Max Temp: 36.1