JAVA后台开发_从入门到精通

7 List排序(sorted)

2019-11-13  本文已影响0人  笑Skr人啊
package com.gp6.list.sort;

import com.gp6.bean.Employee;
import com.gp6.list.utils.ListUtil;

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

/**
* list 排序
*
* @author gp6
* @date 2019-07-23
*/
public class TestSort {

    public static void main(String[] args) {
        List<Employee> employeeList = ListUtil.packEmployeeList();

        // 根据 id(类型:int) 排序
        List<Employee> idSortList = employeeList.stream().sorted(Comparator.comparingInt(Employee::getId)).collect(Collectors.toList());
        idSortList.forEach(employee -> {

        });

        // 根据 薪资(类型:long) 排序
        List<Employee> salarySortList = employeeList.stream().sorted(Comparator.comparingLong(Employee::getSalary)).collect(Collectors.toList());
        salarySortList.forEach(employee -> {

        });
    }
}

上一篇下一篇

猜你喜欢

热点阅读