Try functional type in Java! ①

1. 1. Introduction

Functional interfaces have been provided since java8, but how much do you use them in your products? In the case of a product that is already running, it may be difficult to migrate in many cases, but I felt that there are many merits when actually using it in the product, so this time I actually used the original (functional interface). (No) Let's rewrite the sample code using the functional interface.

2. Functional I / F practice with similar image judgment method

2.1 Processing flow

The following is the processing flow of the similar image judgment method given as an example. It is said that empty is described as a difference, but null is returned if functional I / F is not used, and Optional.empty is returned if it is used.

Similar image judgment method processing flow


Search images from DB by image hash value
      _____________↓____________
↓ Yes ↓ None
Return image data ↓
Search for candidates from additional information in the image
↓ Yes ↓ None
                     ↓                      ↓
Search for similar images ↓
             ________↓_____Empty return
↓ Yes ↓ None
             ↓            ↓
Return similar image data Empty return

2.2 When not using functional I / F

Let's implement the above flow without using functional I / F. Below is a code example.

Functional I/F unused


    /**
     * @param imageHash image hash value
     * @param addInfo Image add information
     * @return 
     */
    public SimilarImageDto getImage(String imageHash, AddInfo addInfo) {
        //Search by exact match
        val image = imageRepository.get(imageHash) 

        //If it exists, generate a Dto and return it
        if ( image != null ) {
             return SimilarImageDto.create(image);

        //Processing when it does not exist
        } else {
            //Search for candidate images from additional information
            List<ImageInfo> imgInfoList = imageInfoRepository.find(addInfo);
            //Similarity judgment from candidates. If it does not exist, null is returned.
            if (CollectionUtils.isEmpty(imgInfoList) ) {
                return null;
            }

            for (ImageInfo imgInfo : imgInfoList) {

                //If a similar image exists, generate a Dto and return it
                if( isSimilar(imgInfo) ){
                    return SimilarImageDto.create(imgInfo);
                }       
            }
            return null;
        }
    }

   //Similar image judgment function
   private boolean isSimilar(ImageInfo imgInfo) = {
       //Similar image search processing
        return isSimilar;
    };

2.3 When not using functional I / F

Let's implement the above flow using functional I / F. Below is a code example.

Functional I/Use F


    /**
     * @param imgHash image hash value
     * @param addInfo Image add information
     * @return 
     */
    public Optional<SimilarImageDto> getImageOpt(String imgHash, AddInfo addInfo) {
        return imageRepository //
          .getOpt(imgHash) //Search by exact match
          .map(img -> SimilarImageDto.create(img) )//If it exists, generate a Dto and return it
          .orElseGet(() -> { //Processing when it does not exist
              return imageInfoRepository.find(addInfo) //Search for candidate images from additional information
                 .detectOptional( imgInfo -> predicateSimilar.test(imgInfo) )//Similarity judgment from candidates. Optional if not present.empty is returned.
                 .map( imgInfo -> SimilarImageDto.create(imgInfo) );//If a similar image exists, generate a Dto and return it
          });
    }

   //Similar image judgment function
   private Predicate<ImageInfo> predicateSimilar = imgInfo -> {
       //Similar image search processing
        return isSimilar;
    };

3. 3. Try using functional I / F

How was it? This time it was a simple flow, but as it gets more complicated, the difference from the case where functional I / F is not used becomes more noticeable. The advantage is that the amount of code is shortened, but when I actually introduce it, I feel that null checking is unnecessary, side effects are reduced by using functions, the scope is naturally reduced, and it is tested. I feel that it becomes simpler as a merit.

Recommended Posts

Try functional type in Java! ①
Type determination in Java
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Implement functional quicksort in Java
Try implementing Android Hilt in Java
Try implementing GraphQL server in Java
Try running Selenuim 3.141.59 in eclipse (java)
Try an If expression in Java
Try running AWS X-Ray in Java
Try to implement Yubaba in Java
Java type conversion
Regarding String type equivalence comparison in Java
Try to solve Project Euler in Java
[Easy-to-understand explanation! ] Reference type type conversion in Java
[JAVA] Stream type
Partization in Java
Try to implement n-ary addition in Java
Try using the Stream API in Java
[Java] Enumeration type
Try Java 8 Stream
Java Optional type
Changes in Java 11
Rock-paper-scissors in Java
Try using JSON format API in Java
Try calling the CORBA service in Java 11+
Java double type
Try making a calculator app in Java
[Java] Functional interface
Pi in Java
Roughly try Java 9
FizzBuzz in Java
Try scraping about 30 lines in Java (CSV output)
The intersection type introduced in Java 10 is amazing (?)
Try to create a bulletin board in Java
About var used in Java (Local Variable Type)
Second decoction: Try an If expression in Java
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Organized memo in the head (Java --Data type)
Try using the COTOHA API parsing in Java
About Java functional interface
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
java standard functional interface
Comments in Java source
[Java] Data type ①-Basic type
Azure functions in java
Try LetCode in Ruby-TwoSum
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java