Java 使用Lambda进行排序无法理解它为什么不能工作
我一直在尝试不同的排序方法,我尝试的方法中有一种没有给出我期望的结果 我从代码中注释掉的Lamda排序中获得了预期的输出Java 使用Lambda进行排序无法理解它为什么不能工作,java,sorting,lambda,Java,Sorting,Lambda,我一直在尝试不同的排序方法,我尝试的方法中有一种没有给出我期望的结果 我从代码中注释掉的Lamda排序中获得了预期的输出 employees.sort(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)); 杰克40 约翰29 约翰30 雪22 蒂姆21 但是从 employees.stream().sorted(Comparator.comparing(Employee::getName).thenC
employees.sort(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge));
杰克40
约翰29
约翰30
雪22
蒂姆21
但是从
employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge));
我得到了不正确的输出
约翰30
约翰29
蒂姆21
杰克40
雪22
我在哪里犯的错误
package com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Employee john = new Employee("John", 30);
Employee john2 = new Employee("John", 29);
Employee tim = new Employee("Tim", 21);
Employee jack = new Employee("Jack", 40);
Employee snow = new Employee("Snow", 22);
List<Employee> employees = new ArrayList<>();
employees.add(john);
employees.add(john2);
employees.add(tim);
employees.add(jack);
employees.add(snow);
employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)); //does not sort original list
//employees.sort(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)); //sorts original list
for(Employee employee : employees){
System.out.println(employee.getName() + " " + employee.getAge());
}
}
}
class Employee{
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
工作示例对原始列表进行排序,但使用stream返回一个新的排序列表。示例-您必须像下面那样收集它,并且employeeList作为其他结果进行排序
final List<Employee> employeeList = employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList());
工作示例对原始列表进行排序,但使用stream返回一个新的排序列表。示例-您必须像下面那样收集它,并且employeeList作为其他结果进行排序
final List<Employee> employeeList = employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList());
.排序不是终端操作。事实上,由于流是惰性的,所以代码行下面没有任何事情发生。要确认,只需添加.peek.system.out::println,您将看到它从未执行过。要排序,请添加terminal operation.collectCollectors.toList
从-
溪流是懒惰的;仅对源数据执行计算
当终端操作启动时,源元素
仅在需要时消费
.排序不是终端操作。事实上,由于流是惰性的,所以代码行下面没有任何事情发生。要确认,只需添加.peek.system.out::println,您将看到它从未执行过。要排序,请添加terminal operation.collectCollectors.toList
从-
溪流是懒惰的;仅对源数据执行计算
当终端操作启动时,源元素
仅在需要时消费
流不修改现有集合。您必须使用终端操作来查看您在流中所做的更改
List<Employee> sortedEmployees = employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList())
流不修改现有集合。您必须使用终端操作来查看您在流中所做的更改
List<Employee> sortedEmployees = employees.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList())
问题应该围绕着溪流而不是lambda。在streams文档中,您会发现的第一件事是操作的类型及其重要性。您的代码没有问题,您只需要进一步了解StreamAPI。您已成功对流进行排序,但未将其重新分配到列表中。在集合上调用stream时,将创建一个stream对象,该对象不引用初始集合。问题应该围绕该流,而不是lambda。在streams文档中,您会发现的第一件事是操作的类型及其重要性。您的代码没有问题,您只需要进一步了解StreamAPI。您已成功对流进行排序,但未将其重新分配到列表中。在集合上调用stream时,将创建一个不引用初始集合的stream对象。