How to replace an element in a list
SolutionFollowing example uses replaceAll() method to replace all the occurance of an element with a different element in a list.
import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six one three Four".split(" ")); System.out.println("List :"+list); Collections.replaceAll(list, "one", "hundread"); System.out.println("replaceAll: " + list); } }Result
The above code sample will produce the following result.
List :[one, Two, three, Four, five, six, one, three, Four] replaceAll: [hundread, Two, three, Four, five, six, hundread, three, Four]
java_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4