Advertisement
valerielashvili

Default_Values2

Jul 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5. import java.util.stream.Stream;
  6.  
  7. public class p02_Default_Values {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String inputLine = scanner.nextLine();
  11.  
  12.         LinkedHashMap<String, String> keyValuePairs = new LinkedHashMap<>();
  13.  
  14.         while (!"end".equals(inputLine)) {
  15.             String[] tokens = inputLine.split(" -> ");
  16.             String key = tokens[0];
  17.             String value = tokens[1];
  18.  
  19.             keyValuePairs.put(key, value);
  20.  
  21.             inputLine = scanner.nextLine();
  22.         }
  23.  
  24.         inputLine = scanner.nextLine();
  25.  
  26.         for (Map.Entry<String, String> keyValuesEntry : keyValuePairs.entrySet()) {
  27.             String k = keyValuesEntry.getKey();
  28.             String v = keyValuesEntry.getValue();
  29.             if ("null".equals(v)) {
  30.                 keyValuePairs.put(k, inputLine);
  31.             }
  32.         }
  33.  
  34.         Stream<Map.Entry<String, String>> orderedKeyValues = keyValuePairs.entrySet().stream()
  35.                 .sorted((a, b) -> {
  36.                     if (a.getValue().compareTo(b.getValue()) == -1) {
  37.                         return 1;
  38.                     } else {
  39.                     return b.getValue().length() - a.getValue().length();
  40.                     }
  41.                 });
  42.  
  43.         for (Map.Entry<String,String> keyValuesOrdered : orderedKeyValues.collect(Collectors.toList())) {
  44.             String key = keyValuesOrdered.getKey();
  45.             String value = keyValuesOrdered.getValue();
  46.  
  47.             System.out.printf("%s <-> %s\n", key, value);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement