使用java 8 流(Stream)对List进行排序

以往的 Collections.sort(); 可以对 String、Integer 等类型的 list 进行排序,但现实中很少用到这种简单的排序,往往集合里存放的都是 自定义的对象 ,那么如何对存放自定义对象的 list 按照自定义的对象的某个属性来排序呢?

示例代码:

自定义的bean:

package com.sc.flightnumminprice.model;

import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;

/**
 * @author zcy
 * @date 2020/3/19
 */
@Setter
@Getter
public class PriceFlightNum {
    private int id;
    private String flightNum;
    private String depAirport;
    private String arrAirport;
    private String currency;
    private BigDecimal minPriceTax;
    private String riskKey;

    @Override
    public String toString() {
        return "PriceFlightNum [riskKey=" + riskKey + ", minPrice=" + minPriceTax + "]";
    }
}

测试类:

package com.sc.flightnumminprice;

import com.sc.flightnumminprice.model.PriceFlightNum;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;

/**
 * @author zcy
 * @date 2020/3/21
 */
@SpringBootTest
public class Test1 {

    @Test
    public void t1(){
        List<PriceFlightNum> list = new ArrayList<>();

        list.add(getPriceFlightNum("PEK","DPS","D7317_QZ557",new BigDecimal("1579.00")));
        list.add(getPriceFlightNum("PEK","DPS","D7317_QZ557",new BigDecimal("1578.00")));

        list.add(getPriceFlightNum("PEK","DPS","D7317_QZ556",new BigDecimal("1578.00")));

        list.add(getPriceFlightNum("ABC","DEF","D7317_QZ556",new BigDecimal("1578.00")));
        list.add(getPriceFlightNum("ABC","DEF","D7317_QZ556",new BigDecimal("1579.00")));
        list.add(getPriceFlightNum("ABC","DEF","D7317_QZ556",new BigDecimal("1566.00")));


        System.out.println("原集合:");
        list.forEach(System.out::println);


		System.out.println("排序(根据 [minPriceTax] 升序排列)集合:");
        List<PriceFlightNum> ascendList = list
                .stream()
                .sorted(Comparator.comparing(PriceFlightNum::getMinPriceTax))
                .collect(toList());
                
		ascendList.forEach(System.out::println);

		System.out.println("排序(根据 [minPriceTax] 降序排列)集合:");
        List<PriceFlightNum> descendList = list
                .stream()
                .sorted(Comparator.comparing(PriceFlightNum::getMinPriceTax).reversed())
                .collect(toList());
                
        descendList.forEach(System.out::println);
        
        
		System.out.println("去重(根据单属性 [flightNum] 去重)集合:");
		List<PriceFlightNum> distinctList1 = newList
                .stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(
                                                Comparator.comparing(
                                                        PriceFlightNum::getFlightNum))), ArrayList::new));
        
        distinctList1.forEach(System.out::println);

		System.out.println("去重(根据多属性 [flightNum_depAirport_arrAirport] 去重)集合:");
        List<PriceFlightNum> distinctList2 = newList
                .stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(
                                                Comparator.comparing(
                                                        o -> o.getFlightNum()+";"+o.getDepAirport()+";"+o.getArrAirport()))), ArrayList::new));

        distinctList2.forEach(System.out::println);


    }

    private PriceFlightNum getPriceFlightNum(String dep,String arr,String flightNum,BigDecimal minPriceTax){
        PriceFlightNum priceFlightNum = new PriceFlightNum();
        priceFlightNum.setDepAirport(dep);
        priceFlightNum.setArrAirport(arr);
        priceFlightNum.setFlightNum(flightNum);

        priceFlightNum.setRiskKey(dep + "_" + arr + "_" + flightNum);
        priceFlightNum.setMinPriceTax(minPriceTax);
        return priceFlightNum;
    }
    
}

输出结果:

原集合:
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ557, minPrice=1579.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ557, minPrice=1578.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ556, minPrice=1578.00]
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1578.00]
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1579.00]
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1566.00]
排序集合:
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1566.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ557, minPrice=1578.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ556, minPrice=1578.00]
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1578.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ557, minPrice=1579.00]
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1579.00]
去重集合:
PriceFlightNum [riskKey=ABC_DEF_D7317_QZ556, minPrice=1566.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ556, minPrice=1578.00]
PriceFlightNum [riskKey=PEK_DPS_D7317_QZ557, minPrice=1578.00]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值