if I want to call a method like this:
List f(List l){
l.add(new Object());
return l;
}
All is fine except if I call the method, it actually modifies its argument, is there anyway around that?
// suppose l is instantiated at this point
log.info(l.count());// prints 0
f(l);
log.info(l.count());// prints 1
is there anyway to declare f in a way to keep l unchanged in java?
I know that I can perform a deep clone on l and pass it, but in cases where l is really big, this operation is expensive.
Use an unmodifiable list:
If you try to modify the list within the method, you will get an
UnsupportedOperationException
.