I wrote a simple program to iterate through a List
using java 8 lambda.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class FirstLamdaExpression {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
//Way 1 : old way
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
System.out.print(t + " ");
}
});
//Way 2
System.out.println(" ");
list.forEach((Integer t) -> System.out.print(t + " "));
//Way 3
System.out.println(" ");
list.forEach((t) -> System.out.print(t + " "));
//Way 4
System.out.println(" ");
list.forEach(System.out::print);
}
}
In the program below I have more than 2 pieces of logic to perform inside lambda. The problem I am facing is how to update the 4th way i.e. System.out::print
?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class SecondLamdaExpression {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
//Way 1 : Old way
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
System.out.print(t + " Twice is : ");
System.out.print(t*2 + " , ");
}
});
//Way 2
System.out.println(" ");
list.forEach((Integer t) ->
{System.out.print(t + " Twice is : ");
System.out.print(t*2 + " , ");
});
//Way 3
System.out.println(" ");
list.forEach((t) -> {System.out.print(t + " Twice is : ");
System.out.print(t*2 + " , ");
});
//Way 4
//System.out.println(" ");
//list.forEach((t)-> System.out::print{(t + " Twice is : ");});
}
}
Java lambda expression is consisted of three components. 1) Argument-list: It can be empty or non-empty as well. 2) Arrow-token: It is used to link arguments-list and body of expression. 3) Body: It contains expressions and statements for lambda expression.
The body of a lambda expression can contain zero, one or more statements. When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.
For example, the given lambda expression takes two parameters and returns their addition. Based on the type of x and y, the expresson will be used differently. If the parameters match to Integer the expression will add the two numbers. If the parameters of type String the expression will concat the two strings. 3.
where lambda operator can be: Please note: Lambda expressions are just like functions and they accept parameters just like functions. Note that lambda expressions can only be used to implement functional interfaces.
It seems you are asking how to pass t + " Twice is : " + t*2 + " , "
to a method reference. You can't pass explicit parameters to method references, and you can't combine method references with lambda expressions.
You could use a Stream
pipeline to first map
t
whatever you wish to print for each value of t
, and than use a forEach
with method reference to print it :
list.stream().map(t -> t + " Twice is : " + t*2 + " , ").forEach(System.out::print);
The fourth way in your code is a method reference. You cannot apply it to multiple statements, or even to methods that accept more than one argument. The only thing I can think of here is extracting this login to a method:
public class SecondLamdaExpression {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// First three ways not repeated here
// Way 4
System.out.println(" ");
list.forEach(SecondLamdaExpression::printTwice);
}
private static void printTwice(Integer t) {
System.out.print(t + " Twice is : ");
System.out.print(t*2 + " , ");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With