fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.Stream;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Test
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. List<Test> tList = new ArrayList<>();
  15. Test t = new Test(2,false);
  16. tList.add(t);
  17. t = new Test(2,true);
  18. tList.add(t);
  19. t = new Test(2,true);
  20. tList.add(t);
  21. t = new Test(2,false);
  22. tList.add(t);
  23. t = new Test(3, true);
  24. t.setB(true);
  25.  
  26. tList.add(t);
  27. t = new Test(10, true);
  28. tList.add(t);
  29. t = new Test(10, false);
  30. tList.add(t);
  31. t = new Test(10, true);
  32. tList.add(t);
  33. t = new Test(10, false);
  34. tList.add(t);
  35. t = new Test(80, false);
  36. tList.add(t);
  37. System.out.println(tList);
  38.  
  39. Comparator<Test> comparator = Comparator.comparing(p -> p.getA());
  40. comparator = comparator.thenComparing(p -> p.getB());
  41.  
  42. tList = tList.stream().sorted(comparator).collect(Collectors.toList());
  43.  
  44. System.out.println(tList);
  45. // List<Test> trGrList = tList.stream().sorted((trgr1, trgr2) -> Boolean.compare(trgr2.getB(),
  46. // trgr1.getB()))
  47. // .collect(Collectors.toList());
  48. // System.out.println(trGrList);
  49.  
  50. // System.out.println(t2.getClass().getName());
  51. // System.out.println(t2.getClass().getName().equals("Test"));
  52.  
  53.  
  54.  
  55. }
  56.  
  57. private static class TransportGroupComparator implements Comparator<Test> {
  58.  
  59. @Override
  60. public int compare(Test trgr1, Test trgr2) {
  61. int result = Boolean.compare(trgr2.getB(),
  62. trgr1.getB());
  63. System.out.println(result);
  64. return result;
  65. }
  66. }
  67.  
  68.  
  69.  
  70.  
  71. private int a;
  72. private boolean b;
  73.  
  74. public Test(){}
  75.  
  76. public Test(int a, boolean b){
  77. super();
  78. this.a = a;
  79. this.b = b;
  80. }
  81.  
  82. public void setA(int a){
  83. this.a = a;
  84. }
  85.  
  86. public int getA(){
  87. return a;
  88. }
  89.  
  90. public void setB(boolean b){
  91. this.b = b;
  92. }
  93.  
  94. public boolean getB(){
  95. return b;
  96. }
  97.  
  98. @Override
  99. public String toString(){
  100. return this.getA() + "||" + this.getB();
  101. }
  102. }
Success #stdin #stdout 0.17s 47544KB
stdin
Standard input is empty
stdout
[2||false, 2||true, 2||true, 2||false, 3||true, 10||true, 10||false, 10||true, 10||false, 80||false]
[2||false, 2||false, 2||true, 2||true, 3||true, 10||false, 10||false, 10||true, 10||true, 80||false]