Java Reference
In-Depth Information
6-1. Writing a Simple Lambda Expres-
sion
Problem
You want to encapsulate a piece of functionality that prints out a simple message.
Solution
Write a lambda expression that accepts a single parameter that contains the message
you want to print, and implement the printing functionality within the lambda. In the
following example, a functional interface, HelloType , is implemented via a lambda
expression and assigned to the variable helloLambda . Lastly, the lambda is invoked,
printing the message.
public class HelloLambda {
/**
* Functional Interface
*/
public interface HelloType {
/**
* Function that will be implemented within the
lambda
* @param text
*/
void hello(String text);
}
public static void main(String[] args){
// Create the lambda, passing a parameter named
"text" to the
// hello() method, returning the string. The
lambda is assigned
// to the helloLambda variable.
HelloType helloLambda =
Search WWH ::




Custom Search