Skip to main content


Showing posts from May 21, 2020

Tail of list.remove()

When I was checking a code I saw this suspicous line,
list.remove(object) However, I'm in differenct team hence this is not related to me. Anyway, I informed the developer, who is a Lead, probably senior to me. Since there team is OK with this and it is working for their use case (but not that safe) I have nothing to do but share the knowledge in my blog.

If you google how to remove a value form a list you sure will hit List.remove(E element). Question is, are you using it correctly?

Working scenario Let's check this out with a String first.
  public void testStringListRemove() {
    List<String> list = new ArrayList<>();
    Assertions.assertEquals(2, list.size());
  } Test is passing. Does that mean i'm wrong. No, Java String uses a pool hence "CD" is same object. This is completely correct.

Failing Scenario Now let's see this …