java stream sorted 多字段排序

原创
04/16 15:03
阅读数 263
package com.qimh.springbootfiledemo;


import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 */
public class OrderList {
    private String orderStatus;
    private Date createDate;
    private String orderNo;

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public OrderList(String orderStatus, Date createDate, String orderNo) {
        this.orderStatus = orderStatus;
        this.createDate = createDate;
        this.orderNo = orderNo;
    }




    public static void main(String[] args) throws ParseException {
        List<OrderList> lists = Lists.newArrayList();
        lists.add(new OrderList("2",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-16"),"0001"));
        lists.add(new OrderList("gc002",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-09"),"0005"));
        lists.add(new OrderList("2",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-15"),"0002"));
        lists.add(new OrderList("gc001",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-11"),"0005"));
        lists.add(new OrderList("0",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-13"),"0003"));
        lists.add(new OrderList("0",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-12"),"0004"));
        lists.add(new OrderList("gc001",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-10"),"0006"));
        lists.add(new OrderList("gc002",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-08"),"0006"));

        //1.订单状态和订单创建时间
        List<OrderList> DiffList = lists.stream().filter(orderList -> "0".equals(orderList.getOrderStatus()) || "2".equals(orderList.getOrderStatus()))
                .sorted(Comparator.comparing(OrderList::getOrderStatus).reversed()
                        .thenComparing(OrderList::getCreateDate).reversed())
                .collect(Collectors.toList());
        System.out.println("DiffList:" + JSON.toJSONString(DiffList));
        //2.订单创建时间
        List<OrderList> myOrderList = lists.stream().filter(orderList -> !"0".equals(orderList.getOrderStatus()) && !"2".equals(orderList.getOrderStatus()))
                .sorted(Comparator.comparing(OrderList::getCreateDate).reversed())
                .collect(Collectors.toList());
        System.out.println("myOrderList:" + JSON.toJSONString(myOrderList));


        List<OrderList> sumOrderList = Lists.newArrayList();
        sumOrderList.addAll(DiffList);
        sumOrderList.addAll(myOrderList);
        System.out.println("sumOrderList:" + JSON.toJSONString(sumOrderList));

//        //2.分组
//        Map<String, List<OrderList>> callRecordMap = lists.stream().collect(Collectors.groupingBy(OrderList::getOrderStatus));
//
//        List<OrderList> tmpList = Lists.newArrayList();
//        for (Map.Entry<String,List<OrderList>> entry:callRecordMap.entrySet()){
//            String Orderstatus = entry.getKey();
//            List<OrderList> orderlists = entry.getValue();
//            System.out.println("Orderstatus:"+Orderstatus);
//            System.out.println("orderlists:"+ JSON.toJSONString(orderlists));
//            tmpList.addAll(orderlists);
//
//        }
//        System.out.println(JSON.toJSONString(tmpList));

    }




}

 

运行结果:

DiffList:[{"createDate":1586707200000,"orderNo":"0003","orderStatus":"0"},{"createDate":1586620800000,"orderNo":"0004","orderStatus":"0"},{"createDate":1586966400000,"orderNo":"0001","orderStatus":"2"},{"createDate":1586880000000,"orderNo":"0002","orderStatus":"2"}]
myOrderList:[{"createDate":1586534400000,"orderNo":"0005","orderStatus":"gc001"},{"createDate":1586448000000,"orderNo":"0006","orderStatus":"gc001"},{"createDate":1586361600000,"orderNo":"0005","orderStatus":"gc002"},{"createDate":1586275200000,"orderNo":"0006","orderStatus":"gc002"}]
sumOrderList:[{"createDate":1586707200000,"orderNo":"0003","orderStatus":"0"},{"createDate":1586620800000,"orderNo":"0004","orderStatus":"0"},{"createDate":1586966400000,"orderNo":"0001","orderStatus":"2"},{"createDate":1586880000000,"orderNo":"0002","orderStatus":"2"},{"createDate":1586534400000,"orderNo":"0005","orderStatus":"gc001"},{"createDate":1586448000000,"orderNo":"0006","orderStatus":"gc001"},{"createDate":1586361600000,"orderNo":"0005","orderStatus":"gc002"},{"createDate":1586275200000,"orderNo":"0006","orderStatus":"gc002"}]

参考连接:

https://www.cnblogs.com/michaelShao/p/10893439.html(楼主说需要实现comparable接口,亲测 未实现comparable接口 不报错)

https://www.cnblogs.com/kuanglongblogs/p/11230250.html

https://www.cnblogs.com/liuheng0315/p/7112239.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部