App下載
首頁(yè)javalambda_apiJava Stream - 如何使用lambda創(chuàng)建Consumer

Java Stream - 如何使用lambda創(chuàng)建Consumer

我們想知道如何使用lambda創(chuàng)建Consumer。
import java.util.function.Consumer;

public class Main {
  public static void main(String... args) {
    Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);
    greeter.accept(new Person("First", "Last"));
  }
}
class Person {
  String firstName;
  String lastName;

  Person() {}

  Person(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
  }
}