Java中的集合 replaceAll() 方法和示例
Java.util.Collections类的replaceAll()方法用于将列表中一个指定值的所有出现替换为另一个。更正式地,用 newVal 替换列表中的每个元素 e,使得
oldVal == null ? e==null : oldVal.equals(e)
Note: This method has no effect on the size of the list.
参数:此方法将以下参数作为参数
- list:要进行替换的列表。
- oldVal:要替换的旧值。
- newVal:要替换 oldVal 的新值。
返回值:如果列表包含一个或多个元素 e,则此方法返回true ,如下所示,否则返回 false
oldVal== null ? e==null : oldVal.equals(e)
句法:
public static boolean replaceAll(List list, T oldVal, T newVal)
示例 1:
Java
// Java program to demonstrate
// replaceAll() method for String value
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating a vector object of string type
List vector = new Vector();
// Populating the above Vector object
// Custom input elements
vector.add("A");
vector.add("B");
vector.add("A");
vector.add("C");
// Printing the vector
System.out.println("Initial Vector :" + vector);
// Replacing value
// using replaceAll() method
Collections.replaceAll(vector, "A", "TAJMAHAL");
// Printing elements of Vector object after
// replacing
System.out.println("Vector after replace :"
+ vector);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message when exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to demonstrate
// replaceAll() method for Integer value
// importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating object of List
List vector = new Vector();
// Populate the vector
vector.add(20);
vector.add(30);
vector.add(20);
vector.add(30);
// Printing the vector before replacing
// elements
System.out.println("Initial values are :"
+ vector);
// Replacing value
// using replaceAll() method
Collections.replaceAll(vector, 20, 400);
// Printing the vector after replacing elements
System.out.println("Value after replace :"
+ vector);
}
// Catch block to handle IllegalArgumentException
catch (IllegalArgumentException e) {
// Display the exceptions on the console
System.out.println("Exception thrown : " + e);
}
}
}
输出
Initial Vector :[A, B, A, C]
Vector after replace :[TAJMAHAL, B, TAJMAHAL, C]
示例 2:
Java
// Java program to demonstrate
// replaceAll() method for Integer value
// importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating object of List
List vector = new Vector();
// Populate the vector
vector.add(20);
vector.add(30);
vector.add(20);
vector.add(30);
// Printing the vector before replacing
// elements
System.out.println("Initial values are :"
+ vector);
// Replacing value
// using replaceAll() method
Collections.replaceAll(vector, 20, 400);
// Printing the vector after replacing elements
System.out.println("Value after replace :"
+ vector);
}
// Catch block to handle IllegalArgumentException
catch (IllegalArgumentException e) {
// Display the exceptions on the console
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Initial values are :[20, 30, 20, 30]
Value after replace :[400, 30, 400, 30]