RGAlex

Test

Mar 30th, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Test {
  4.     public static void main(String[] args) {
  5.         Map<String, int[]> test = new LinkedHashMap<>();
  6.         test.put("A", new int[2]);
  7.         test.get("A")[0] = 3;
  8.         test.get("A")[1] = 2;
  9.         test.put("B", new int[2]);
  10.         test.get("B")[0] = 4;
  11.         test.get("B")[1] = 1;
  12.         test.put("C", new int[2]);
  13.         test.get("C")[0] = 3;
  14.         test.get("C")[1] = 4;
  15.         test.put("D", new int[2]);
  16.         test.get("D")[0] = 3;
  17.         test.get("D")[1] = 2;
  18.         //<- Резултатът от .sorted((a, b) е в низходящ ред !!!
  19.         test.entrySet().stream().sorted((a, b) -> {
  20.             int result = Integer.compare(b.getValue()[0], a.getValue()[0]);//<-Сортираме по първата стойност
  21.             if (result == 0) { //<-Ако тя е равна
  22.                 result = a.getKey().compareTo(b.getKey());//<-Сортираме по ключ
  23.             }
  24.             return result;
  25.         }).forEach(e -> System.out.printf("%s%n key1: %d%n key2: %d%n",
  26.                 e.getKey(), e.getValue()[0], e.getValue()[1]));
  27.         System.out.println("--------");
  28.        
  29.         test.entrySet().stream().sorted((a, b) -> {
  30.             int result = Integer.compare(b.getValue()[1], a.getValue()[1]);//<-Сортираме по втората стойност
  31.             if (result == 0) {//<-Ако тя е равна
  32.                 result = a.getKey().compareTo(b.getKey());//<-Сортираме по ключ
  33.             }
  34.             return result;
  35.         }).forEach(e -> System.out.printf("%s%n key1: %d%n key2: %d%n",
  36.                 e.getKey(), e.getValue()[0], e.getValue()[1]));
  37.         System.out.println("--------");
  38.        
  39.         test.entrySet().stream().filter(a -> a.getValue()[1] >= 2)//<-Филтрираме втората стойност да е >=2
  40.                 .sorted((a, b) -> {
  41.             int result = Integer.compare(b.getValue()[1], a.getValue()[1]);//<-Сортираме по втората стойност
  42.             if (result == 0) {//<-Ако тя е равна
  43.                 result = result = Integer.compare(b.getValue()[0], a.getValue()[0]);//<-Сортираме по първата стойност
  44.                 if (result == 0) {//<-Ако тя е равна
  45.                     result = a.getKey().compareTo(b.getKey());//<-Сортираме по ключ
  46.                 }
  47.             }
  48.             return result;
  49.         }).forEach(e -> System.out.printf("%s%n key1: %d%n key2: %d%n",
  50.                 e.getKey(), e.getValue()[0], e.getValue()[1]));
  51.    
  52.     }
  53. }              
  54.        // Конзолата отпечатва:
  55.        //B
  56.        // key1: 4
  57.        // key2: 1
  58.        //A
  59.        // key1: 3
  60.        // key2: 2
  61.        //C
  62.        // key1: 3
  63.        // key2: 4
  64.        //D
  65.        // key1: 3
  66.        // key2: 2
  67.        //--------
  68.        //C
  69.        // key1: 3
  70.        // key2: 4
  71.        //A
  72.        // key1: 3
  73.        // key2: 2
  74.        //D
  75.        // key1: 3
  76.        // key2: 2
  77.        //B
  78.        // key1: 4
  79.        // key2: 1
  80.        //--------
  81.        //C
  82.        // key1: 3
  83.        // key2: 4
  84.        //A
  85.        // key1: 3
  86.        // key2: 2
  87.        //D
  88.        // key1: 3
  89.        // key2: 2
  90.  
  91.  
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment