英文:
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<Employee> byStreetName = (emp1, emp2) -> emp1.getAddress().getName().compareTo(
emp2.getAddress().getName());
and use it like :
myList.sort(byStreetName);
or if you are using streams
myList.stream().sorted(byStreetName)...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论