Ajinkya Kothe’s Post

Removing Elements from ArrayList Removing Elements from ArrayList – Do It Correctly Many developers write: for(String s : list) { list.remove(s); // ❌ Wrong } This throws: 👉 ConcurrentModificationException Because ArrayList iterator is fail-fast. Correct ways: ✔ Use Iterator: Iterator<String> it = list.iterator(); while(it.hasNext()) { it.remove(); } #Java #ArrayList #CollectionsFramework #BackendDevelopment #Programming #JavaDeveloper #CleanCode #SoftwareEngineering #TechGrowth #SpringBoot #day45ofJavaandSpringboot

to avoid that we can use CopyOnWriteArrayList<>()

To view or add a comment, sign in

Explore content categories