Use Java lambda expressions outside of the Stream API

Use Java lambda expressions outside of the Stream API

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.

Concrete example

For example, take a look at the ResultSetExtractor used by Spring jdbc. This Interface has only the following methods:

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.

Summary

Recommended Posts

Use Java lambda expressions outside of the Stream API
The origin of Java lambda expressions
Nowadays Java lambda expressions and Stream API
Java: Use Stream to sort the contents of the collection
[java8] To understand the Stream API
About Lambda, Stream, LocalDate of Java8
How to use Java lambda expressions
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Try using the Stream API in Java
Java Stream API
How to use Java API with lambda expression
[For beginners] About lambda expressions and Stream API
[Java] Stream API / map
Understand Java 8 lambda expressions
Java8 Stream API practice
About Java lambda expressions
Explain Java 8 lambda expressions
[Java Silver] Summary of points related to lambda expressions
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] Sort the list using streams and lambda expressions
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Use Lambda Layers with Java
[Java11] Stream Summary -Advantages of Stream-
Java Stream API cheat sheet
[Java] Summary of regular expressions
Java Stream API in 5 minutes
Java8 stream, lambda expression summary
[Java] Introduction to lambda expressions
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Introduction to Stream API
Use Redis Stream in Java
[Java] Stream API intermediate operation
[Must-see for apprentice java engineer] How to use Stream API
I want to use the Java 8 DateTime API slowly (now)
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
Java: The problem of which is faster, stream or loop
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Java lambda expressions learned with Comparator
[Java] Delete the elements of List
[Introduction to Java] About lambda expressions
[Java version] The story of serialization
[Introduction to Java] About Stream API
Find out the list of fonts available in AWS Lambda + Java
I tried the input / output type of Java Lambda ~ Map edition ~
Let's consider the meaning of "stream" and "collect" in Java's Stream API.
A story about hitting the League Of Legends API with JAVA
I tried using Java8 Stream API
Parsing the COTOHA API in Java
Basic processing flow of java Stream
Getting started with Java lambda expressions
Java 8 ~ Stream API ~ to start now
Do you use Stream in Java?
Leverage Either for individual exception handling in the Java Stream API
A little understanding of lambda expressions
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
What wasn't fair use in the diversion of Java APIs on Android
How to convert an array of Strings to an array of objects with the Stream API
[Java] Generate a narrowed list from multiple lists using the Stream API