Java ArrayBlockingQueue 類的 forEach() 方法對 Iterable 的每個元素執行指定的動作,直到所有元素都被處理或動作拋出異常。
用法:
public void forEach(Consumer<? super E> action)
參數:
action- 這是要為每個元素執行的操作。
指定者:
ArrayBlockingQueue 類的 forEach() 方法由接口 Iterable<E> 中的 forEach() 方法指定。
拋出:
如果定義的操作表示 null,則 forEach() 方法拋出 NullPointerException。
例子1
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ArrayBlockingQueueForEachExample1 {
public static void main(String[] args) {
int capacity =100;
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity);
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.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
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";
}
}
public class ArrayBlockingQueueForEachExample2 {
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");
int capacity = 100;
BlockingQueue<Student> queue = new ArrayBlockingQueue<Student>(capacity);
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);
}
}
}
輸出:
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.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ArrayBlockingQueueForEachExample3 {
static int i =0;
static int capacity =100;
public static void main(String[] args) {
// create a ArrayBlockingQueue
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity);
// Adding items add() to the tail of queue
queue.add("aman");
queue.add("bhavika");
//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 = aman Upper case = AMAN 2 Lower case = bhavika Upper case = BHAVIKA
示例 4
import java.util.Collections;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ArrayBlockingQueueForEachExample4 {
public static void main(String[] args) {
Integer[] val = {0, 1};
int capacity = 100;
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
queue.add(71018);
queue.add(8000);
queue.add(1178);
queue.add(1190);
//foreach() will perform the given action for each element
for (Integer n:queue) {
System.out.println(n);
val[0] = Collections.max(queue);
val[1] = Collections.min(queue);
}
System.out.println("Max number = "+ val[0]);
System.out.println("Min number = "+ val[1]);
}
}
輸出:
71018 8000 1178 1190 Max number = 71018 Min number = 1178
相關用法
- Java ArrayBlockingQueue peek()用法及代碼示例
- Java ArrayBlockingQueue put()用法及代碼示例
- Java ArrayBlockingQueue Spliterator()用法及代碼示例
- Java ArrayBlockingQueue drainTo()用法及代碼示例
- Java ArrayBlockingQueue toArray()用法及代碼示例
- Java ArrayBlockingQueue contains()用法及代碼示例
- Java ArrayBlockingQueue take()用法及代碼示例
- Java ArrayBlockingQueue removeIf()用法及代碼示例
- Java ArrayBlockingQueue clear()用法及代碼示例
- Java ArrayBlockingQueue iterator()用法及代碼示例
- Java ArrayBlockingQueue spliterator()用法及代碼示例
- Java ArrayBlockingQueue offer()用法及代碼示例
- Java ArrayBlockingQueue toString()用法及代碼示例
- Java ArrayBlockingQueue add()用法及代碼示例
- Java ArrayBlockingQueue remainingCapacity()用法及代碼示例
- Java ArrayBlockingQueue remove()用法及代碼示例
- Java ArrayBlockingQueue retainAll()用法及代碼示例
- Java ArrayBlockingQueue poll()用法及代碼示例
- Java ArrayBlockingQueue size()用法及代碼示例
- Java ArrayBlockingQueue removeAll()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ArrayBlockingQueue forEach() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。