RGAlex

Test2

Mar 31st, 2021 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Test2 {
  4.     public static void main(String[] args) {
  5.         Map<String, Map<String, List<Double>>> test = new TreeMap<>();
  6.         test.put("B", new TreeMap<>() {{
  7.             put("Pounds", new ArrayList<>());
  8.         }});
  9.         test.get("B").get("Pounds").add(65.00);
  10.         test.get("B").get("Pounds").add(62.50);
  11.  
  12.         test.put("C", new TreeMap<>() {{
  13.             put("Pounds", new ArrayList<>());
  14.         }});
  15.         test.get("C").get("Pounds").add(65.00);
  16.         test.get("C").get("Pounds").add(62.50);
  17.         test.get("C").get("Pounds").add(7.50);
  18.  
  19.         test.put("A", new TreeMap<>() {{
  20.             put("Pounds", new ArrayList<>());
  21.             ;
  22.         }});
  23.         test.get("A").get("Pounds").add(41.00);
  24.         test.get("A").get("Pounds").add(43.00);
  25.  
  26.         test.put("D", new TreeMap<>() {{
  27.             put("Pounds", new ArrayList<>());
  28.             ;
  29.         }});
  30.         test.get("D").get("Pounds").add(41.00);
  31.         test.get("D").get("Pounds").add(44.00);
  32.         test.get("D").get("Pounds").add(24.00);
  33.  
  34.         test.entrySet()
  35.                 .stream()
  36.                 .sorted(Map.Entry.comparingByKey())//<-Сортираме по първия ключ във възходящ ред
  37.                 .forEach(e -> System.out.printf("%s - have %.2f %s%n",
  38.                         e.getKey(),//<-Първия ключ
  39.                         e.getValue().get(e.getValue().keySet().toString().replaceAll("[\\[\\]]", ""))//<-Сумираме стойностите
  40.                                 .stream()
  41.                                 .mapToDouble(Double::doubleValue)
  42.                                 .sum(),
  43.                         e.getValue().keySet().toString().replaceAll("[\\[\\]]", ""))); //<-Взимаме втория ключ
  44.         System.out.println("-----------------------------");
  45.  
  46.         test.entrySet()
  47.                 .stream()
  48.                 .sorted((a, b) -> { //<-Сортираме по сумата от стойностите в низходящ ред
  49.                     return Double.compare(b.getValue().get(b.getValue().keySet().toString()
  50.                                     .replaceAll("[\\[\\]]", ""))
  51.                                     .stream().mapToDouble(Double::doubleValue).sum(),
  52.                             a.getValue().get(a.getValue().keySet().toString()
  53.                                     .replaceAll("[\\[\\]]", "")).stream()
  54.                                     .mapToDouble(Double::doubleValue).sum());
  55.                 }).filter(c -> c.getValue().get(c.getValue().keySet().toString()//<-Взимаме само тези със сума >=90
  56.                 .replaceAll("[\\[\\]]", ""))
  57.                 .stream().mapToDouble(Double::doubleValue).sum() >= 90)
  58.                 .forEach(e -> System.out.printf("%s - have %.2f %s%n",
  59.                         e.getKey(),//<-Първия ключ
  60.                         e.getValue().get(e.getValue().keySet().toString().replaceAll("[\\[\\]]", ""))//<-Сумираме стойностите
  61.                                 .stream()
  62.                                 .mapToDouble(Double::doubleValue)
  63.                                 .sum(),
  64.                         e.getValue().keySet().toString().replaceAll("[\\[\\]]", "")));//<-Взимаме втория ключ
  65.         System.out.println("-----------------------------");
  66.  
  67.         test.entrySet().stream().sorted((a, b) -> {//<-Сортираме по първата стойност  в низходящ ред
  68.             int sort = Double.compare(b.getValue().get(b.getValue().keySet().toString()
  69.                             .replaceAll("[\\[\\]]", "")).get(0),
  70.                     a.getValue().get(a.getValue().keySet().toString()
  71.                             .replaceAll("[\\[\\]]", "")).get(0));
  72.             if (sort == 0) {//<- Ако първата стойност е равна сортираме по втарата
  73.                 sort = Double.compare(b.getValue().get(b.getValue().keySet().toString()
  74.                                 .replaceAll("[\\[\\]]", "")).get(1),
  75.                         a.getValue().get(a.getValue().keySet().toString()
  76.                                 .replaceAll("[\\[\\]]", "")).get(1));
  77.                 if (sort == 0) {//<- Ако втората стойност е равна сортираме първия ключ
  78.                     Map.Entry.comparingByKey();
  79.                 }
  80.             }
  81.             return sort;
  82.         }).forEach(e -> System.out.printf("%s - have %.2f and %.2f %s%n",
  83.                 e.getKey(),//<-Първия ключ
  84.                 e.getValue().get(e.getValue().keySet().toString().replaceAll("[\\[\\]]", "")).get(0),//<-Първа стойност
  85.                 e.getValue().get(e.getValue().keySet().toString().replaceAll("[\\[\\]]", "")).get(1),//<-Втора стойност
  86.                 e.getValue().keySet().toString().replaceAll("[\\[\\]]", "")));//<-Втория ключ
  87.     }
  88. }
  89.        // Конзолата отпечатва:
  90.        // A - have 84,00 Pounds
  91.        // B - have 127,50 Pounds
  92.        // C - have 135,00 Pounds
  93.        // D - have 109,00 Pounds
  94.        // -----------------------------
  95.        // C - have 135,00 Pounds
  96.        // B - have 127,50 Pounds
  97.        // D - have 109,00 Pounds
  98.        // -----------------------------
  99.        // B - have 65,00 and 62,50 Pounds
  100.        // C - have 65,00 and 62,50 Pounds
  101.        // D - have 41,00 and 44,00 Pounds
  102.        // A - have 41,00 and 43,00 Pounds
Add Comment
Please, Sign In to add comment