Advertisement
KeepCoding

07. Sums by Town

Apr 6th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.LinkedHashMap;
  7.  
  8. public class P07SumsByTown {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         int n = Integer.parseInt(reader.readLine());
  12.         HashMap<String, Double> townIncome = new HashMap<>();
  13.         for (int i = 0; i < n; i++) {
  14.             String[] currentInput = Arrays.stream(reader.readLine().split("\\|")).map(w -> w.trim()).toArray(String[]::new);
  15.             String currentTown = currentInput[0];
  16.             Double currentIncome = Double.parseDouble(currentInput[1]);
  17.             townIncome.putIfAbsent(currentTown, 0.0);
  18.             townIncome.put(currentTown, townIncome.get(currentTown) + currentIncome);
  19.         }
  20.  
  21.         townIncome.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).forEach(e -> {
  22.             System.out.printf("%s -> %.1f%n", e.getKey(), e.getValue());
  23.         });
  24.         //main ends here
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement