In Guava, is there an efficient way to add or remove items to an ImmutableList
(creating new Lists in the process, of course).
The simplest way I can come up with is this:
private ImmutableList<String> foos = ImmutableList.of();
public void addFoo(final String foo) {
if (this.foos.isEmpty()) {
foos = ImmutableList.of(foo);
} else {
foos = ImmutableList.<String>builder().addAll(foos).add(foo).build();
}
}
public void removeFoo(final String foo) {
final int index = this.foos.indexOf(foo);
if (index > -1) {
final Builder<String> builder = ImmutableList.<String>builder();
if (index > 0) builder.addAll(this.foos.subList(0, index));
final int size = this.foos.size();
if (index < size - 1) builder.addAll(this.foos.subList(index+1, size));
this.foos = builder.build();
}
}
What I would like to avoid doing is this:
public void removeFoo(final String foo) {
final ArrayList<String> tmpList = Lists.newArrayList(this.foos);
if(tmpList.remove(foo))this.foos=ImmutableList.copyOf(tmpList);
}
But unfortunately it is so much simpler than any Guava-only method I can think of. Have I missed something?
When you don't expect to modify a collection, it's a good practice to defensively copy it into an immutable collection. Immutable lists have many advantages over their mutable siblings, like they are thread-safe, more memory-efficient, and can be passed to untrusted libraries without any side effects.
ImmutableList, as suggested by the name, is a type of List which is immutable. It means that the content of the List are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown.
In Java 8 and earlier versions, we can use collection class utility methods like unmodifiableXXX to create immutable collection objects. If we need to create an immutable list then use the Collections. unmodifiableList() method.
List is mutable AFAIK, but Guava has some immutable collections. ArrayList is mutable. Not all implementations of List are.
You can remove by filtering, which doesn't create an intermediate ArrayList
or builder, and only traverses the list once:
public void removeFoo(final String foo) {
foos = ImmutableList.copyOf(Collections2.filter(foos,
Predicates.not(Predicates.equalTo(foo)));
}
For adding, I don't see a better solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With