I'm assuming the method of the functional interface implemented by this lambda expression has a return value, so when using brackets, it should include a return statement, just like any method with non-void return type.
new JdbcTemplate(new SingleConnectionDataSource(c, true))
.query("select id, name from PLAYERS", (rs, rowNum) ->
{
return new Player(rs.getString("id"), rs.getString("name");
})
);
Answer from Eran on Stack OverflowI'm assuming the method of the functional interface implemented by this lambda expression has a return value, so when using brackets, it should include a return statement, just like any method with non-void return type.
new JdbcTemplate(new SingleConnectionDataSource(c, true))
.query("select id, name from PLAYERS", (rs, rowNum) ->
{
return new Player(rs.getString("id"), rs.getString("name");
})
);
Don't do that. Having multiple statements in a lambda in most cases is a code smell. Rather create a method with two parameters:
private Player toPlayer(ResultSet rs, int rowNum) {
// multiple setters here
return player;
}
And then pass the method reference (which in fact will behave like a BiFunction) instead of lambda:
new JdbcTemplate(new SingleConnectionDataSource(c, true))
.query("select id, name from PLAYERS", this::toPlayer);
One may want to create a static utility method instead of a dynamic one. The logic is the same as above:
public class MappingUtil {
// ...
public static Player toPlayer(ResultSet rs, int rowNum) {
// multiple setters here
return player;
}
}
And then:
new JdbcTemplate(new SingleConnectionDataSource(c, true))
.query("select id, name from PLAYERS", MappingUtil::toPlayer);
WARNING
As mentioned in comments, Using peek() for production code is considered bad practice
The reasson is that "According to its JavaDocs, the intermediate Stream operation java.util.Stream.peek() “exists mainly to support debugging” purposes."
As a consequence, this proposed solution SHOULD NOT be used.
Forgot to relate to the first code snippet. I wouldn't use forEach at all. Since you are collecting the elements of the Stream into a List, it would make more sense to end the Stream processing with collect. Then you would need peek in order to set the ID.
List<Entry> updatedEntries =
entryList.stream()
.peek(e -> e.setTempId(tempId))
.collect (Collectors.toList());
For the second snippet, forEach can execute multiple expressions, just like any lambda expression can :
entryList.forEach(entry -> {
if(entry.getA() == null){
printA();
}
if(entry.getB() == null){
printB();
}
if(entry.getC() == null){
printC();
}
});
However (looking at your commented attempt), you can't use filter in this scenario, since you will only process some of the entries (for example, the entries for which entry.getA() == null) if you do.
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
//lambda
//Output : A,B,C,D,E
items.forEach(item->System.out.println(item));
//Output : C
items.forEach(item->{
System.out.println(item);
System.out.println(item.toLowerCase());
}
});
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 + " , ");
}
}