首页javalambda_apiJava Stream - 如何使用Consumer输出并做计算

Java Stream - 如何使用Consumer输出并做计算

我们想知道如何使用Consumer输出并做计算。
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    List<Employee> employees = Arrays.asList(
        new Employee("John", 22_000),
        new Employee("Mark", 11_000));

    acceptAllEmployee(employees, e -> System.out.println(e.name));
    acceptAllEmployee(employees, e -> {
      e.salary *= 1.5;
    });
    acceptAllEmployee(employees,
        e -> System.out.println(e.name + ": " + e.salary));
  }
  public static void acceptAllEmployee(List<Employee> employees,
      Consumer<Employee> printer) {
    for (Employee e : employees) {
      printer.accept(e);
    }
  }
}

class Employee {
  public String name;
  public double salary;

  Employee(String name, double salary) {
    this.name = name;
    this.salary = salary;
  }
}