siebz0r siebz0r - 1 year ago 91
Java Question

Lombok lazy getter for mutable collections

I have a class which has a collection:

public class Foo
private BarManager barManager;
@Getter(lazy = true)
private final List<Bar> bars = barManager.getAll();

public void addBar(Bar bar)

However I cannot add/remove elements to/from the
. The cause is that the attribute is an
. The warning/error is:

The method add(Employee) is undefined for the type AtomicReference<AtomicReference<List<Employee>>>

How can perform add/remove operations on the collection?

Answer Source

Your solution is weird indeed and depends on some implementation details. Moreover it break with NPE if the field hasn't been initialized yet. The proper solution works always: