Java Reference
In-Depth Information
ReverseType newText = (testText) -> {
String tempStr = "";
for (String part : testText.split(" ")) {
tempStr = new
StringBuilder(part).reverse().toString();
}
return tempStr;
};
The following code could be used to invoke the lambda expression:
System.out.println(newText.reverse("HELLO"));
Result:
OLLEH
Solution 2
Use a functional interface that is contained within the java.util.function pack-
age to implement a lambda expression to suit the needs of the application. The follow-
ing example uses the Function<T,R> interface to perform the same task as the one
contained in solution 1. This example accepts a string argument and returns a string
result.
Function<String,String> newText2 = (testText) -> {
String tempStr = "";
for (String part : testText.split(" ")) {
tempStr = new
StringBuilder(part).reverse().toString();
}
return tempStr;
};
This lambda expression is assigned to the variable newText2 , which is of type
Function<String,String> . Therefore, a string is passed as an argument, and a
string is to be returned from the lambda expression. The functional interface of Func-
Search WWH ::




Custom Search