veronikaaa86

TestPair

Feb 27th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package exams.july2017;
  2.  
  3. import javafx.util.Pair;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9. import java.util.LinkedHashMap;
  10. import java.util.Map;
  11.  
  12. public class P04_PokemonEvolution {
  13.     public static void main(String[] args) throws IOException {
  14.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15.  
  16.         String input = "";
  17.  
  18.         Map<String, ArrayList<Pair<String, Integer>>> map = new LinkedHashMap<>();
  19.         String oneName = "";
  20.         String pokemon = "";
  21.         while (true) {
  22.             input = reader.readLine();
  23.             if (input.equals("wubbalubbadubdub")) break;
  24.  
  25.             String[] tokens = input.split(" -> ");
  26.             if (tokens.length == 1) {
  27.                 oneName = tokens[0];
  28.             } else {
  29.  
  30.                 pokemon = tokens[0];
  31.                 String evoType = tokens[1];
  32.                 int evoIndex = Integer.parseInt(tokens[2]);
  33.  
  34.                 if (!map.containsKey(pokemon)) {
  35.                     map.put(pokemon, new ArrayList<>());
  36.                     map.get(pokemon).add(new Pair<>(evoType, evoIndex));
  37.                 } else {
  38.                     map.get(pokemon).add(new Pair<>(evoType, evoIndex));
  39.                 }
  40.             }
  41.  
  42.             if (map.containsKey(oneName)) {
  43.                 System.out.println("# " + oneName);
  44.                 map.get(oneName)
  45.                         .forEach((value) -> {
  46.                             System.out.printf("%s <-> %d%n", value.getKey(), value.getValue());
  47.                         });
  48.             }
  49.         }
  50.  
  51.         map.forEach((key, value) -> {
  52.             System.out.printf("# %s%n", key);
  53.             value.stream()
  54.                     .sorted((x1, x2) -> Integer.compare(x2.getValue(), x1.getValue()))
  55.                     .forEach(ordered -> {
  56.                         System.out.printf("%s <-> %d%n", ordered.getKey(), ordered.getValue());
  57.                     });
  58.         });
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment