Search code examples
javajava-8builderlombok

Builder with lambda expression for Java 8


I have two class with me as mentioned below. I wanted to create two instances of class A.

I want to create instance obj2 from existing instance obj1 with updated value of a3 attribute as "Java".

I had tried below line using Builder, but it's not working.

A obj2 = obj1.builder().a3("Java").build();

I am able to do it with calling constructor, but I wanted to do it Builder pattern only.

@Builder(toBuilder = true)
@Data
class A {
    String a1;
    String a2;
    String a3;
    B b;

    A(String b1, String b2, String b3, B b) {
        this.a1 = b1;
        this.a2 = b2;
        this.a3 = b3;
        this.b = b;
    }
}

@Builder
@Data
class B {
    String b1;
    String b2;
    String b3;

    B(String b1, String b2, String b3) {
        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;
    }

}

class Main {
    public static void main(String[] args) {

        B b = new B("a", "b", "b");
        A obj1 = new A("a1", "b1", "b1", b);
        A obj2 = new A("x1", "y1", "z1", b);
        List<A> list= new ArrayList<>();
        list.add(obj1);
        list.add(obj2);
        list.forEach(a -> {
            a.toBuilder().a1("newA1").a2("newA2").build();
            repository.save(a);
        });

        A obj3 = obj1.toBuilder().a3("Java").build();
    }
}

As mentioned in updated code , i have list of A with me, and i want to update a1 and a2 attributes of all element in list using builder. But builder is not working fine with lambda. If i will use setter with below code it's working fine.

list.forEach(a -> {
    a.setA1("newA1");
    a.setA2("newA2");
});

I am not able to get updated values for a1 and a2 in case of Builder with Lambda


Solution

  • This statement doesn't do anything

    list.forEach(a -> {
        a.toBuilder().a1("newA1").a2("newA2").build();  //<<<
        repository.save(a);
    });
    

    You are creating a new object from the old one and then throwing it away. It will not make changes to the existing object. Consider

    list.stream()
        .map(a -> a.toBuilder().a1("newA1").a2("newA2").build())
        .forEach(repository::save);