[Java] Handling of JavaBeans in the method chain

I thought about how to handle objects created in JavaBeans in the method chain.

JavaBeans specification

I don't know if it's the exact specification, but the common JavaBeans specifications are as follows.

There are many other things, but the important one is this one. Click here for official specifications → JavaBeans Spec


public class HogeBean {

    private String name;

    private String id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getId() {
        return id;

    public void setId(String id) {
        this.id = id;

Like this. With lombok


public class HogeBean {

    private String name;

    private String id;

This is OK.

Problems with the method chain

For example, suppose you have a HogeBean and a FugaBean with similar fields.


public class HogeBean {

    private String name;

    private String id;


public class FugaBean {

    private String id;

    private String password;

If you want to convert HogeBean to FugaBean in Stream, think normally

List<HogeBean> hoges = ...

hoges.stream().map(hoge -> {

    FugaBean fuga = new FugaBean();
    return fuga;

I will write only like this. It's not so beautiful.

Solution 1 Anonymous class + instance initializer

It may not be very beautiful, but the curly braces + return in the map is a preventable pattern.

hoges.stream().map(hoge -> new FugaBean(){

It's much cleaner than before. But there are drawbacks

Solution 2 Make the setter return itself so that you can create an object in one line.

The option to quit JavaBeans. Modify the setter as follows.


    public HogeBean setId(String id) {
        this.id = id;
        return this;

For lombok, just add Accessors (chain = true)


@Accessors(chain = true)

The processing of the case can be done as follows

hoges.stream().map(hoge -> new FugaBean().setId(hoge.getId())).Termination

Refreshing. But after all there are drawbacks,

Solution 3 Make a little builder

Like this


public final class BeanCreater<T> {

    private BeanCreater() {

    private T bean;

    public static <T> BeanCreater<T> of(Supplier<T> supplier) {

        BeanCreater<T> builder = new BeanCreater<T>();
        builder.bean = supplier.get();

        return builder;

    public BeanCreater<T> construct(Consumer<T> consumer) {

        return this;
    public T build() {
        return this.bean;


With this,

hoges.stream().map(hoge -> BeanCreater.of(FugaBean::new)
    .construct(bean -> bean.setId(hoge.getId())).build()).Termination

Hmm. I tried it, but it's not good enough. The code is too noisy.


Personally, I like Solution 2 the most. It has a flowing interface, and there is no waste in essence and ritual talk. I hope JavaBeans will be more flexible considering the current situation. (I don't think that's the case ...) Click here for the BeanCreater created this time https://github.com/7tsuno/BeanCreater

Please advise if you have any other ideas!

Recommended Posts

[Java] Handling of JavaBeans in the method chain
Method name of method chain in Java Builder + α
Call the super method in Java
What is the main method in Java?
The story of writing Java in Emacs
[Order method] Set the order of data in Rails
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
Import files of the same hierarchy in Java
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Get the URL of the HTTP redirect destination in Java
[Java] Get the file in the jar regardless of the environment
Change the storage quality of JPEG images in Java
Summarize the additional elements of the Optional class in Java 9
About the handling of Null
[Java] Practice of exception handling [Exception]
Implementation of gzip in java
Benefits of Java static method
Implementation of tri-tree in Java
Exception handling techniques in Java
[Java] Integer information of characters in a text file acquired by the read () method
How to get the class name / method name running in Java
Was done in the base year of the Java calendar week
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
A quick explanation of the five types of static in Java
[Android, Java] Convenient method to calculate the difference in days
Create a method to return the tax rate in Java
Count the number of digits after the decimal point in Java
How to derive the last day of the month in Java
Summary of values returned by the Spliterator characteristics method #java
Access the network interface in Java
[Java] Delete the elements of List
Guess the character code in Java
[Java version] The story of serialization
Automatic photo resizing method in Java
Specify the java location in eclipse.ini
Java comparison using the compareTo () method
Unzip the zip file in Java
Handling of time zones using Java
[Note] Handling of Java decimal point
List of members added in Java 9
About the role of the initialize method
Parsing the COTOHA API in Java
Step-by-step understanding of Java exception handling
List of types added in Java 9
Order of processing in the program
The origin of Java lambda expressions
Implementation of like function in Java
Handling of java floating point [Note] while reading the reference book
Examine the system information of AWS Lambda operating environment in Java
[Java] Get the dates of the past Monday and Sunday in order
Output the difference between each field of two objects in Java
Find out the list of fonts available in AWS Lambda + Java
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java