method reference
A compact way to pass an existing method as a function, written Type::method, such as String::toUpperCase or User::active.
In more detail
A method reference behaves like a lambda that only calls that method. In stream pipelines it names which accessor or conversion each step uses, which often reads more clearly than a lambda.
Examples
.map(String::toUpperCase).flatMap(List::stream)Related terms
- lambda expression — A short anonymous function written inline and passed to other code, such as a condition for a filter.
- functional interface — An interface with exactly one abstract method, such as Runnable, Supplier or Predicate, which a lambda or method reference can implement.
Taught in
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- The Streams API — Modern Java: Virtual Threads & High-Throughput Services
- Advanced Stream Operations — Modern Java: Virtual Threads & High-Throughput Services
- Async With CompletableFuture — Modern Java: Virtual Threads & High-Throughput Services
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lists, Sets, Maps, Deques And Sequenced Collections — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Exceptions, Arrays And Collections — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Reduction, Grouping And Partitioning — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Lambdas And Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep