ConcurrentLinkedQueue 類的 forEach() 方法對 Iterable 的每個元素執行指定的 action,直到處理完所有元素,或者 action 拋出異常。
用法:
public void forEach(Consumer<? super E> action)
參數:
action- 這是要為每個元素執行的操作。
指定者:
ConcurrentLinkedQueue 類的 forEach() 方法指定為:
接口 Iterable<E> 中的 forEach() 方法。
拋出:
如果定義的操作表示 null,則 forEach() 方法拋出 NullPointerException。
例子1
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueForEachExample1 {
public static void main(String[] args) {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
queue.add("Reema");
queue.add("Rahul");
queue.add("Rita");
queue.add("Ramesh");
System.out.println(queue);
System.out.println("\nPrinting the queue with the help of foreach loop:");
//foreach() will perform the given action for each element
for (String xyz:queue) {
System.out.println(xyz);
}
}
}
輸出:
[Reema, Rahul, Rita, Ramesh] Printing the queue with the help of foreach loop: Reema Rahul Rita Ramesh
例子2
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueForEachExample2 {
public static void main(String[] args) {
Student student1 = new Student("15cs1029", "MVN University", "Reema");
Student student2 = new Student("15cs1010", "MVN University", "Geetanjali");
Student student3 = new Student("17cs1029", "MR University", "Vineet");
Student student4 = new Student("15cs1011", "MVN University", "Himanshu");
ConcurrentLinkedQueue<Student> queue = new ConcurrentLinkedQueue<Student>();
queue.add(student1);
queue.add(student2);
queue.add(student3);
queue.add(student4);
//foreach() will perform the given action for each element
for (Student xyz:queue) {
System.out.println(xyz);
}
}
}
class Student {
static int i = 1;
public String name;
public String college;
String rollNo;
Student(String rollNo, String college, String name) {
this.rollNo = rollNo;
this.name = name;
this.college = college;
}
public String toString() {
return i++ + ".Roll No. = " + this.rollNo + "\n College = " + this.college + "\n Name = " + this.name + "\n";
}
}
輸出:
1.Roll No. = 15cs1029 College = MVN University Name = Reema 2.Roll No. = 15cs1010 College = MVN University Name = Geetanjali 3.Roll No. = 17cs1029 College = MR University Name = Vineet 4.Roll No. = 15cs1011 College = MVN University Name = Himanshu
例子3
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueForEachExample3 {
static int i =0;
public static void main(String[] args) {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
queue.add("reema");
queue.add("sonia");
//foreach() will perform the given action for each element
for (String val:queue) {
String upperCase = val.toUpperCase();
System.out.println(++i + " Lower case = " + val);
System.out.println(" Upper case = " + upperCase);
System.out.println();
}
}
}
輸出:
1 Lower case = reema Upper case = REEMA 2 Lower case = sonia Upper case = SONIA
示例 4
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueForEachExample4 {
public static void main(String[] args) {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
queue.add(null);
System.out.println("\nPrinting the queue with the help of foreach loop:");
//foreach() will perform the given action for each element
for (String xyz:queue) {
System.out.println(xyz);
}
}
}
輸出:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920) at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327) at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297) at com.javaTpoint.ConcurrentLinkedQueueForEachExample4.main(ConcurrentLinkedQueueForEachExample4.java:7)
相關用法
- Java ConcurrentLinkedQueue removeAll()用法及代碼示例
- Java ConcurrentLinkedQueue add()用法及代碼示例
- Java ConcurrentLinkedQueue offer()用法及代碼示例
- Java ConcurrentLinkedQueue spliterator()用法及代碼示例
- Java ConcurrentLinkedQueue size()用法及代碼示例
- Java ConcurrentLinkedQueue iterator()用法及代碼示例
- Java ConcurrentLinkedQueue contains()用法及代碼示例
- Java ConcurrentLinkedQueue retainAll()用法及代碼示例
- Java ConcurrentLinkedQueue removeIf()用法及代碼示例
- Java ConcurrentLinkedQueue remove()用法及代碼示例
- Java ConcurrentLinkedQueue peek()用法及代碼示例
- Java ConcurrentLinkedQueue isEmpty()用法及代碼示例
- Java ConcurrentLinkedQueue addAll()用法及代碼示例
- Java ConcurrentLinkedQueue poll()用法及代碼示例
- Java ConcurrentLinkedQueue toArray()用法及代碼示例
- Java ConcurrentLinkedDeque add()用法及代碼示例
- Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代碼示例
- Java ConcurrentLinkedDeque removeLast()用法及代碼示例
- Java ConcurrentLinkedDeque hashCode()用法及代碼示例
- Java ConcurrentLinkedDeque contains()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ConcurrentLinkedQueue forEach() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。