How do you usually do collection getters in Java? For example I have two classes.
public class A
{
String s = "1";
A () {}
}
public class B
{
List<A> list;
public B()
{
list = new ArrayList<>();
}
public List<A> getList()
{
return list;
}
}
If I have getter like this, I can do something like:
B elem = new B();
elem.getList().add(new A());
So it breaks the idea of a getter.
I can do
public List<A> getList()
{
return new ArrayList<>(list);
}
But still, if I modify some elements of result, the original element will be changed.