List.replaceAll(UnaryOperator操作符) 方法在 Java 中
List 接口的 replaceAll() 方法接受一个表示特定操作的 UnaryOperator 对象,对当前列表的所有元素执行指定的操作,并将列表中的现有值替换为它们各自的结果。
示例
import java.util.ArrayList;
import java.util.function.UnaryOperator;
class Op implements UnaryOperator<String> {
public String apply(String str) {
return str.toUpperCase();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("JavaScript");
list.add("CoffeeScript");
list.add("HBase");
list.add("OpenNLP");
System.out.println("Contents of the list: "+list);
list.replaceAll(new Op());
System.out.println("Contents of the list after replace operation: \n"+list);
}
}输出
Contents of the list: [Java, JavaScript, CoffeeScript, HBase, OpenNLP] Contents of the list after replace operation:[JAVA, JAVASCRIPT, COFFEESCRIPT, HBASE, OPENNLP]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP