เมธอด replaceAll() ของอินเทอร์เฟซ List ยอมรับอ็อบเจ็กต์ของ 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]