繁体   English   中英

基于自定义参数Java的对象列表排序

[英]Sorting of List of Objects based on Custom parameter Java

我正在编写一个组件,它需要对 Object 变量参数进行排序。 例如:Employee 有变量 lastName。 我想按 lastName.lenght() 对员工进行排序 我看过很多例子,但我找不到答案 我能以这种方式实现结果还是我们需要不同的方法。

我举个例子

public class Employee {
    private int Id;
    private String firstName;
    private String lastName;
    private String department;

    public Employee(int id, String firstName, String lastName, String department) {
        Id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.department = department;
    }

    public Employee() {
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "Id=" + Id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", department='" + department + '\'' +
                '}';
    }
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class SortExample
{
    public static void main(String[] args)
    {
        ArrayList<Employee> employees = getUnsortedEmployeeList();
        Comparator<Employee> compareByName = Comparator
                .comparing(Employee::getFirstName).reversed();

        Comparator<Employee> compareByLastName = Comparator
                .comparing(Employee::getLastName).reversed();


        
        List<Employee> sortedEmployees = employees.stream()
                .sorted(compareByName)
                .sorted(compareByLastName)
        //I have commented the code
        //.sorted((e1,e2) ->e1.getLastName().length().compareTo(e2.getLastName().length()))
                .collect(Collectors.toList());

        System.out.println(sortedEmployees);
    }

    private static ArrayList<Employee> getUnsortedEmployeeList()
    {
        ArrayList<Employee> list = new ArrayList<>();
        list.add( new Employee(2, "Irene", "Parr","programming") );
        list.add( new Employee(1, "Nicola", "Mitchell","programming") );
        list.add( new Employee(4, "Emily", "Carr","finance") );
        list.add( new Employee(5, "Blake", "Baker","finance") );
        list.add( new Employee(3, "Natalie", "Parsons","marketing") );
        list.add( new Employee(7, "Olivia", "Clarkson","marketing") );
        list.add( new Employee(6, "Justin", "Scott","marketing") );

        return list;
    }
}

任何帮助,将不胜感激。

您可能想熟悉Comparator接口的 Java 8 static 方法,例如 compare comparing() 、 comparisonInt comparingInt()等。

List<Employee> sortedEmployees = employees.stream()
    .sorted(Comparator.comparingInt(employee -> employee.getLastName().length()))
    .collect(Collectors.toList());

此外,不要对数据进行多次排序(即避免多次应用sorted() ),而是可以通过使用不同风格的方法thenComparing()来链接比较器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM