I tried Java Lambda input / output type ~ Stream version ~

Various patterns can be used for Java Lambda input / output types.

In the official documentation, it is written here.

In POJO, I introduced how to handle JSON using POJO classes.

Finally, it is the Stream edition using InputStream / OutputStream.

It is a code that is modified from the previous POJO edition and Parse and Generate by itself using Jackson.

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

public class StreamFunction implements RequestStreamHandler {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        Product product = OBJECT_MAPPER.readValue(inputStream, Product.class);

        String name = product.getName();
        System.out.println(name); // orange juice

        Integer price = product.getPrice();
        System.out.println(price); // 1000

        Long releaseDate = product.getReleaseDate();
        System.out.println(releaseDate); // 1601606387939

        Double rate = product.getRate();
        System.out.println(rate); // 4.5

        List<String> rawMaterial = product.getRawMaterial();
        System.out.println(rawMaterial); // [orange, fragrance]

        Product.Size size = product.getSize();
        Integer height = size.getHeight();
        System.out.println(height); // 10

        Integer width = size.getWidth();
        System.out.println(width); // 20

        Integer depth = size.getDepth();
        System.out.println(depth); // 30

        Product response = new Product();
        response.setName("coffee");
        response.setPrice(Integer.valueOf(500));
        response.setReleaseDate(System.currentTimeMillis());
        response.setRate(Double.valueOf(4.2));
        response.setRawMaterial(Arrays.asList("coffee", "sugar"));

        Product.Size responseSize = new Product.Size();
        responseSize.setHeight(Integer.valueOf(100));
        responseSize.setWidth(Integer.valueOf(200));
        responseSize.setDepth(Integer.valueOf(300));
        response.setSize(responseSize);

        OBJECT_MAPPER.writeValue(outputStream, response);
    }
}

When executed, the result is the same as the POJO edition.

{
  "name": "coffee",
  "price": 500,
  "releaseDate": 1601627790764,
  "rate": 4.2,
  "rawMaterial": [
    "coffee",
    "sugar"
  ],
  "size": {
    "height": 100,
    "width": 200,
    "depth": 300
  }
}

I don't think I'll use it much, but maybe when I'm dealing with huge JSON or when I want to use the JSON Parser / Generator library.

Which one is better after all

For product codes, it is recommended to use the POJO method for maintainability. If you just want to move it quickly and try it out, the Map method is easy.

Recommended Posts

I tried Java Lambda input / output type ~ Stream version ~
I tried Java Lambda input / output type ~ POJO edition ~
I tried the input / output type of Java Lambda ~ Map edition ~
I tried using Java8 Stream API
[JAVA] Stream type
I tried to summarize Java lambda expressions
I tried to output multiplication table in Java
Java8 stream, lambda expression summary
I tried Drools (Java, InputStream)
I tried using Java REPL
I tried node-jt400 (SQL stream)
I tried metaprogramming in Java
I tried to convert a string to a LocalDate type in Java
[Java] I want to test standard input & standard output with JUnit
About Lambda, Stream, LocalDate of Java8
I tried to interact with Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried using WebAssembly Stadio (2018/4/17 version)
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
I tried using Elasticsearch API in Java
I tried Cassandra's Object Mapper for Java
Java9 was included, so I tried jshell.
Nowadays Java lambda expressions and Stream API
I tried the new era in Java
I tried using OpenCV with Java + Tomcat
I tried to summarize the Stream API
I tried Google's entrance exam (unofficial) [java]
I've reviewed Java's lambda expression, stream API, six months after I started Java.
I can't remember the text file input / output in Java, so I summarized it.
Let's write Java file input / output with NIO
I tried putting Java on my Mac easily
I tried to make Basic authentication with Java
[Rails] I tried to raise the Rails version from 5.0 to 5.2
[Java] How to get and output standard input
java I tried to break a simple block
I tried to implement deep learning in Java
I tried to create Alexa skill in Java
I tried to break a block with java (1)
I tried running Java on a Mac terminal
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
Try the free version of Progate [Java I]