I'm sure many people use Java lambda expressions with the Stream API, Lambda expressions can be used not only in the interfaces newly added in java8 such as Consumer and Function. Lambda expressions can be used with functional Interfaces, And the Interface in which only one method is defined in Java is a functional Interface.
For example, take a look at the ResultSetExtractor
T extractData(ResultSet rs)
Therefore, up to java7, the code written as follows
Entity entity = jdbc.query(sql,new ResultSetExtractor<Entity>() {
@Override
public Entity extractData(ResultSet rs) throws SQLException,
DataAccessException {
if(!rs.next()){
return null;
}else{
Entity result = new Entity();
//setValue from resultSet
return result;
}
});
});
You can write as follows.
Entity entity = jdbc.query(sql, rs -> {
if(!rs.next()){
return null;
}else{
Entity result = new Entity();
//setValue from resultSet
return result;
}
}
});
It's very refreshing. However, if you are not accustomed to it, you may not know what kind of anonymous class you are making. It depends on the team situation.
Recommended Posts