Home > database >  Set object properties more efficiently
Set object properties more efficiently

Time:05-10

Is there a way to set object properties more efficiently than this if I have more than 50 properties?

    object.setProperty1(value1);
    object.setProperty2(value2);
    object.setProperty3(value3);
    object.setProperty4(value4);
    object.setProperty5(value5);
              .
              .
              .

CodePudding user response:

If you don’t want to use the constructor, you could use the builder pattern. The final result would look something like this:

Object.getBuilder()
    .property1(a)
    .property2(b)
    .build();

CodePudding user response:

There are Bean mapping frameworks like MapStruct, which allow to map fields between pojo's automatically.

CodePudding user response:

You can use Spring BeanUtils to handle it

  • Related