Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:44:22+00:00 2026-06-13T01:44:22+00:00

In Guava, is there an efficient way to add or remove items to an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:44:23+00:00Added an answer on June 13, 2026 at 1:44 am

    The ConcurrentModificationException isn’t really related to concurrency and synchronization. Accessing a mutable List concurrently might corrupt it and/or throw the exception (be prepared to all 3 possibilities). You code can’t fail this way, but with multithreading it doesn’t work either:

    • Without synchronization and without foos being volatile, there’s no guarantee that another thread will ever see the changes you’ve made.
    • Even with volatile, it can happen that some changes get lost, e.g., when two threads add an item to foos, both of them can start with the original value and then the one writing last wins (and only its item gets added).

    The code you’re trying to avoid is nothing to be avoided.

    • “I have to create superfluous intermediate collections” – yes, but there’s no free lunch:
      • determine the size of the result in advance, which means an additional iteration through the whole list
      • or allocate a large enough array and copy the needed range in the resulting list
      • or allocate a large enough array and use only part of it (saving time and wasting memory)
      • or create an immutable view (saving both time and memory, but possibly losing time later)
    • AFAIK Frank’s answer implements the first possibility, which is fine if the predicate is fast.
    • “I have to mix java.util Collections with guava ImmutableCollections, while I’d like to stick to one paradigm.” – yes, but for mutating a collection a mutable collection is needed. The ImmutableList.Builder covers just the most common cases allowing to handle them in a compact way.

    You might want to have a look at persistent collections, which are optimized for such operations. However, you shouldn’t expect e.g. the persistent list to be as fast as an ArrayList or ImmutableList.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more efficient way to create a string array from Guava's Splitter
Is there a way of achieving the below using Guava? //anything better than using
In Java, is there a short elegant way to combine multiple predicates (Guava Predicate)
Is there a flatten method in Guava - or an easy way to convert
I would like to know if there is currently a way with Guava MapMaker
Is there any way to pause event posting by the EventBus from the guava
There are a few factory methods in Google Guava to create InputSuppliers, e.g. from
In Guava, if I have a Collection<T> - is there any existing function in
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();
Is there any way how to get the key (or the whole entry) from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.