Replace value in a list
static<T> boolean replaceAll(List<T> list, T oldVal, T newVal)
- Replaces all occurrences of one specified value in a list with another.
import java.util.Collections;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
v.add("A");
v.add("C");
v.add("D");
System.out.println(v);
Collections.replaceAll(v, "A", "java2s.com");
System.out.println(v);
}
}
The output:
[A, B, A, C, D]
[java2s.com, B, java2s.com, C, D]
Home
Java Book
Collection
Java Book
Collection
Collections:
- Collections
- Get empty collection from Collections
- Do binary search
- Copy value to a List
- Get Enumeration from collection, create list from Enumeration
- Fill a list
- Get the max and min value from a Collection
- Fill n Copy object to a list
- Replace value in a list
- Reverse a list
- Get comparator in reverse order
- Rotate and shuffle a list
- Create singleton
- Sort a list
- Swap element in a list
- Get a synchronized collection
- Return an unmodifiable view of collections