java8 sort orderBy

More can refer to: http: //www.importnew.com/15259.html

Test :

com.icil.report.jdbc Package; 

Import of java.util.ArrayList; 
Import java.util.Comparator; 
Import java.util.List; 
Import java.util.stream.Collectors; 

Import org.junit.Test; 

Import com.icil .report.pojo.GroupByCourier; 

public  class OrderByTest { 
  
  / * * 
   * Java7 ordinary sorting 
   * multi-condition combination Sort: // NumberOfShipments sorted according to the first, then more Courier sort 
   * of JDK 8, we can now put more Comparator chain together (chain together) to build a more complex comparison logic : 
   * @throws Exception 
   * / 
  @Test 
  public  void orderBYtest1 () throws Exception { 
      the ArrayList <GroupByCourier> = the arrayList new new ArrayList<GroupByCourier>();
//       GroupByCourier(String courier, Integer numberOfShipments, Integer numberOfSubscriptions)
      arrayList.add(new GroupByCourier("f",2,3));
      arrayList.add(new GroupByCourier("b",10,6));
      arrayList.add(new GroupByCourier("a",4,1));
      arrayList.add(new GroupByCourier("c",15,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",19,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",1,8));
      
   arrayList.sort(Comparator.comparing(GroupByCourier::getNumberOfShipments).thenComparing(GroupByCourier::getCourier));
   //先根据NumberOfShipments 排序,然后在更具 Courier 排序
   Comparator.comparing(GroupByCourier::getNumberOfShipments).thenComparing(GroupByCourier::getCourier);
   arrayList.forEach(r->{System.out.println(r.getCourier() +" -- "+r.getNumberOfShipments()+"--"+r.getNumberOfSubscriptions ());}); 
      
  } 
  
  
  / * * 
   * as Java7 parallel streams ordering 
   * Sort plurality condition combination: // The first NumberOfShipments sorting, and sorting in more Courier, then sorted according NumberOfSubscriptions 
   * start from JDK 8 , we can now comparator chain together multiple (chain together) to build a more complex comparison logic: 
   * @throws Exception 
   * / 
  @Test 
  public  void orderBYtest02 () throws Exception { 
      the ArrayList <GroupByCourier> = the arrayList new new the ArrayList <GroupByCourier> ();
 //        GroupByCourier (String Courier, numberOfShipments Integer, Integer numberOfSubscriptions) 
      arrayList.add ( new new GroupByCourier ( " f ",2,3));
      arrayList.add(new GroupByCourier("b",10,6));
      arrayList.add(new GroupByCourier("a",4,1));
      arrayList.add(new GroupByCourier("c",15,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",19,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",1,8));
      
      //先根据NumberOfShipments 排序,然后在更具 Courier 排序 ,然后在根据 NumberOfSubscriptions 排序
    //List<GroupByCourier> collect = arrayList.parallelStream().sorted(Comparator.comparing(GroupByCourier::getNumberOfShipments).thenComparing(GroupByCourier::getCourier)).collect(Collectors.toList());
    List<GroupByCourier> collect = arrayList.parallelStream().sorted(Comparator.comparing(GroupByCourier::getNumberOfShipments).thenComparing(GroupByCourier::getCourier).thenComparing(GroupByCourier::getNumberOfSubscriptions)).collect(Collectors.toList());
      
      System.err.println(collect);
      collect.forEach(r->{System.out.println(r.getCourier() +" -- "+r.getNumberOfShipments()+"- " + r.getNumberOfSubscriptions ());}); 
      
  } 
  / * * 
   * java8 parallel streams ordering reversal order 
   * the JDK. 8 also provides a useful method for reversing Comparator (reverse Comparator) - we can use it to quickly reverse our sorting 
   * multi-condition combination Sort: // NumberOfShipments sorted according to the first, then more Courier sorting and then sorted according to NumberOfSubscriptions 
   * JDK 8 from the beginning, we can now put more Comparator chains with (chain together) to build a more complex comparison logic: 
   * @throws Exception 
   * / 
  @Test 
  public  void orderBYtest04 () throws Exception { 
      the ArrayList <GroupByCourier> = the arrayList new new the ArrayList <GroupByCourier> ();
 //       GroupByCourier(String courier, Integer numberOfShipments, Integer numberOfSubscriptions)
      arrayList.add(new GroupByCourier("f",2,3));
      arrayList.add(new GroupByCourier("b",10,6));
      arrayList.add(new GroupByCourier("a",4,1));
      arrayList.add(new GroupByCourier("c",15,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(new GroupByCourier("c",19,8));
      arrayList.add(new GroupByCourier("c",1,8));
      arrayList.add(newGroupByCourier ( " C " , . 1 , . 8 )); 
      
      // first sorted according NumberOfShipments, then sort more Courier, then sorted according NumberOfSubscriptions (now turn) 
      Comparator <GroupByCourier> = compars . Comparator Comparing (GroupByCourier :: . getNumberOfShipments) thenComparing (. :: GroupByCourier getCourier) thenComparing (GroupByCourier :: getNumberOfSubscriptions); 
      List <GroupByCourier> = the collect arrayList.parallelStream () the sorted (. compars.reversed () ) .collect (Collectors.toList ()); 
      
      the System .err.println (the collect); 
      collect.forEach (R & lt -> {the System.out.println(r.getCourier() +" -- "+r.getNumberOfShipments()+"--"+r.getNumberOfSubscriptions());});
  }
  
    
}

 

Guess you like

Origin www.cnblogs.com/lshan/p/10956495.html