Lombok @Builder not initializing collections

Only when you use @Singular, you get an empty list. On the Builder documentation page it says:

…with the @Singular annotation, lombok will treat that builder node as a collection.

Without the @Singular, lombok treats it as any other object. So it will be null instead of an empty Collection.

Disclosure: I am a Lombok developer


Since Lombok v1.16.16, you can use @Builder‘s inner annotation to initialize your collection by default. For example:

@Data
@Builder
class Movie {

@Builder.Default
private final List<Actor> actors = new ArrayList<>();

}

So when you create a new Movie using the builder, without specifying any actors:

Movie movie = Movie.builder().build();
movie.getActors(); // Will return instance of ArrayList

Tags:

Java

Lombok