Skip to main content

Posts

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.   @Test   public void testStringListRemove() {     List<String> list = new ArrayList<>();     list.add("AB");     list.add("CD");     list.add("EF");     list.remove("CD");     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. Fa