Collections: Collections.replaceAll swaps every matching element

Collections.replaceAll(list, old, new) replaces every occurrence of one value with another throughout the list, unlike List.replaceAll which takes a transforming function.

Code
List<String> tags = new ArrayList<>(List.of("todo", "done", "todo", "blocked"));
Collections.replaceAll(tags, "todo", "pending");
System.out.println("After replaceAll: " + tags);
Output
After replaceAll: [pending, done, pending, blocked]
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms