Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding and removing items to a Guava ImmutableList

Tags:

java

guava

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?

like image 512
Sean Patrick Floyd Avatar asked Oct 17 '12 15:10

Sean Patrick Floyd


People also ask

Why use ImmutableList?

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.

What does mean that list is immutable?

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.

How do you make a collection immutable?

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.

Is list mutable in Java?

List is mutable AFAIK, but Guava has some immutable collections. ArrayList is mutable. Not all implementations of List are.


1 Answers

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.

like image 183
Frank Pavageau Avatar answered Sep 17 '22 10:09

Frank Pavageau