java8 排序 orderBy

更多可以参考:http://www.importnew.com/15259.html

Test :

package com.icil.report.jdbc;

import 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 普通排序
   * 多条件组合排序: //先根据NumberOfShipments 排序,然后在更具 Courier 排序
   * 从JDK 8开始,我们现在可以把多个Comparator链在一起(chain together)去建造更复杂的比较逻辑:
   * @throws Exception
   */
  @Test
  public void orderBYtest1() throws Exception {
      ArrayList<GroupByCourier> arrayList = 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());});
      
  }
  
  
  /**
   *  java7 并行流式排序
   * 多条件组合排序: //先根据NumberOfShipments 排序,然后在更具 Courier 排序 ,然后在根据 NumberOfSubscriptions 排序
   * 从JDK 8开始,我们现在可以把多个Comparator链在一起(chain together)去建造更复杂的比较逻辑:
   * @throws Exception
   */
  @Test
  public void orderBYtest02() throws Exception {
      ArrayList<GroupByCourier> arrayList = 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));
      
      //先根据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 并行流式排序  反转排序
   *  JDK 8同样提供了一个有用的方法用来反转Comparator(reverse Comparator)——我们可以快速地利用它来反转我们的排序
   * 多条件组合排序: //先根据NumberOfShipments 排序,然后在更具 Courier 排序 ,然后在根据 NumberOfSubscriptions 排序
   * 从JDK 8开始,我们现在可以把多个Comparator链在一起(chain together)去建造更复杂的比较逻辑:
   * @throws Exception
   */
  @Test
  public void orderBYtest04() throws Exception {
      ArrayList<GroupByCourier> arrayList = 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));
      
      //先根据NumberOfShipments 排序,然后在更具 Courier 排序 ,然后在根据 NumberOfSubscriptions 排序 (现在是反过来)
      Comparator<GroupByCourier>  compars = Comparator.comparing(GroupByCourier::getNumberOfShipments).thenComparing(GroupByCourier::getCourier).thenComparing(GroupByCourier::getNumberOfSubscriptions);
      List<GroupByCourier> collect = arrayList.parallelStream().sorted(compars.reversed()).collect(Collectors.toList());
      
      System.err.println(collect);
      collect.forEach(r->{System.out.println(r.getCourier() +" -- "+r.getNumberOfShipments()+"--"+r.getNumberOfSubscriptions());});
  }
  
    
}

猜你喜欢

转载自www.cnblogs.com/lshan/p/10956495.html