根据自定义对象列表中的一个字段进行排序。

huangapple go评论70阅读模式
英文:

Sort object list based on custom object as one of field

问题

Sure, here's the translated code part:

public class Employee {

    private String name;
    private String id;
    private Address address;

    public String getName() {
        return name;
    }

    public Employee setName(String name) {
        this.name = name;
        return this;
    }

    public String getId() {
        return id;
    }

    public Employee setId(String id) {
        this.id = id;
        return this;
    }

    public Address getAddress() {
        return address;
    }

    public Employee setAddress(Address address) {
        this.address = address;
        return this;
    }
}

public class Address {

    public String streetName;
    public String streetNumber;

    public String getStreetName() {
        return streetName;
    }

    public Address setStreetName(String streetName) {
        this.streetName = streetName;
        return this;
    }

    public String getStreetNumber() {
        return streetNumber;
    }

    public Address setStreetNumber(String streetNumber) {
        this.streetNumber = streetNumber;
        return this;
    }
}

To sort a List<Employee> based on the streetName field within the address field, you can use a Java lambda expression and a custom comparator. Here's how you can do it:

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

public class Main {
    public static void main(String[] args) {
        List<Employee> employees = ...; // Populate the list of employees

        List<Employee> sortedEmployees = employees.stream()
                .sorted(Comparator.comparing(e -> e.getAddress().getStreetName()))
                .collect(Collectors.toList());

        // Now, sortedEmployees contains the list of employees sorted by streetName
    }
}

Please replace ... in the code with the actual list of Employee objects that you want to sort. The lambda expression e -> e.getAddress().getStreetName() specifies the key for sorting, which is the streetName field within the address field of each Employee.

英文:
public class Employee {

private String name;
private String id;
private Address address;

public String getName() {
        return name;
    }

    public Employee setName(String name) {
        this.name = name;
        return this;
    }


    public String getId() {
        return id;
    }

    public Employee setId(String id) {
        this.id = id;
        return this;
    }


    public Address getAddress() {
        return address;
    }

    public Employee setName(Address address) {
        this.address = address;
        return this;
    }


}

public class Address{
	
	public String streetName;
	public String streetNumber;

public String getName() {
        return streetName;
    }

    public Address setStreetName(String streetName) {
        this.streetName = streetName;
        return this;
    }


    public String getId() {
        return id;
    }

    public Address setStreetNumber(String streetNumber) {
        this.streetNumber = streetNumber;
        return this;
    }

}

Employee is one of class as defined above and Address is one of data type for one of field for Employee. List<Employee> needs to be sorted based on streetName which is part of address field of Employee.

This can be done using writing a custom comparator. I am looking for a way using java lambda to sort and get the sorted list?

答案1

得分: 2

你可以像下面这样创建一个比较器,或者像@Hadi J在评论中所做的那样:

Comparator<Employee> byStreetName = (emp1, emp2) -> emp1.getAddress().getName().compareTo(
                                                    emp2.getAddress().getName());

然后可以这样使用:

myList.sort(byStreetName);

或者如果你在使用流:

myList.stream().sorted(byStreetName)...
英文:

You could create a comparator like below or the way @Hadi J does in the comment:

Comparator&lt;Employee&gt; byStreetName = (emp1, emp2) -&gt; emp1.getAddress().getName().compareTo(
                                                    emp2.getAddress().getName());

and use it like :

myList.sort(byStreetName);

or if you are using streams

myList.stream().sorted(byStreetName)...

huangapple
  • 本文由 发表于 2020年10月6日 01:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64213289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定