Java通过steam实现数据排序

1、添加依赖

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.3</version>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.16.10</version>
	<scope>provided</scope>
</dependency>

2、测试实体类

package com.shucha.deveiface.biz.dto;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author tqf
 * @Description
 * @Version 1.0
 * @since 2022-07-07 14:24
 */
@Data
public class SortData {
    @ApiModelProperty(value = "姓名")
    private String name;
    @ApiModelProperty(value = "年龄")
    private Integer age;
    @ApiModelProperty(value = "年份")
    private String yearStr;
}

3、多种排

package com.shucha.deveiface.biz.test;

import com.alibaba.fastjson.JSON;
import com.shucha.deveiface.biz.dto.SortData;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tqf
 * @Description java8新特性stream进行处理-排序
 * @Version 1.0
 * @since 2022-07-07 14:26
 */
public class SortDataTest {
    public static void main(String[] args) {
        String nameArray[] = {"张三","李四","王五","谭海","王杰"};
        List<SortData> list = new ArrayList<>();
        for (int i=0;i<5;i++) {
            SortData sortData = new SortData();
            sortData.setAge(i+10);
            sortData.setName(nameArray[i]);
            sortData.setYearStr(String.valueOf(i));
            list.add(sortData);
        }
        // 1. Comparator.comparing(类::属性一).reversed();
        // 2. Comparator.comparing(类::属性一,Comparator.reverseOrder());

        test(list);
        test1(list);
        test2(list);
        test3(list);
        test4(list);
        test5(list);
    }

    /**
     * 对象集合以类属性一升序排序
     * @param list
     */
    public static void test(List<SortData> list){
        // list.stream().sorted(Comparator.comparing(类::属性));
        list = list.stream().sorted(Comparator.comparing(SortData::getAge))
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(list));
    }

    /**
     * 先以属性一升序,结果进行属性一降序  2种写法
     * @param list
     */
    public static void test1(List<SortData> list){
        // 1、先以年龄升序,结果进行年龄降序 list.stream().sorted(Comparator.comparing(类::属性).reversed())
        List<SortData> result = list.stream().sorted(Comparator.comparing(SortData::getAge).reversed())
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result));

        // 2、以年龄降序 list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()))
        List<SortData> result2 = list.stream().sorted(Comparator.comparing(SortData::getAge, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result2));
    }

    /**
     * 对象集合以类属性一升序 属性二升序
     * @param list
     */
    public static void test2(List<SortData> list) {
        // list.stream().sorted(Comparator.comparing(类::属性一).thenComparing(类::属性二));
        list = list.stream().sorted(Comparator.comparing(SortData::getAge)
                .thenComparing(SortData::getYearStr))
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(list));
    }

    /**
     * 对象集合以类属性一降序 属性二升序 注意两种写法
     * @param list
     */
    public static void test3(List<SortData> list) {
        // 先以属性一升序,升序结果进行属性一降序,再进行属性二升序
        // list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二));
        List<SortData> result = list.stream().sorted(Comparator.comparing(SortData::getAge).reversed()
                .thenComparing(SortData::getYearStr)).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result));

        // 先以属性一降序,再进行属性二升序
        // list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()).thenComparing(类::属性二));
        List<SortData> result2 = list.stream().sorted(Comparator.comparing(SortData::getAge,Comparator.reverseOrder())
                .thenComparing(SortData::getYearStr)).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result2));
    }

    /**
     * 对象集合以类属性一降序 属性二降序 注意两种写法
     * @param list
     */
    public static void test4(List<SortData> list){
        // 先以属性一升序,升序结果进行属性一降序,再进行属性二降序
        // list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二,Comparator.reverseOrder()));
        List<SortData> result = list.stream().sorted(Comparator.comparing(SortData::getAge).reversed()
                .thenComparing(SortData::getYearStr,Comparator.reverseOrder())).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result));

        // 先以属性一降序,再进行属性二降序
        // list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()).thenComparing(类::属性二,Comparator.reverseOrder()));
        List<SortData> result2 = list.stream().sorted(Comparator.comparing(SortData::getAge,Comparator.reverseOrder())
                .thenComparing(SortData::getYearStr,Comparator.reverseOrder())).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result2));
    }

    /**
     * 对象集合以类属性一升序 属性二降序 注意两种写法
     * @param list
     */
    public static void test5(List<SortData> list) {
        // 先以属性一升序,升序结果进行属性一降序,再进行属性二升序,结果进行属性一降序属性二降序
        // list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二).reversed());
        List<SortData> result = list.stream().sorted(Comparator.comparing(SortData::getAge).reversed()
                .thenComparing(SortData::getYearStr).reversed()).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result));

        // 先以属性一升序,再进行属性二降序
        // list.stream().sorted(Comparator.comparing(类::属性一).thenComparing(类::属性二,Comparator.reverseOrder()));
        List<SortData> result2 = list.stream().sorted(Comparator.comparing(SortData::getAge)
                .thenComparing(SortData::getYearStr,Comparator.reverseOrder())).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result2));
    }
}
posted @ 2022-08-04 19:11  码奴生来只知道前进~  阅读(108)  评论(0编辑  收藏  举报