当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayBlockingQueue forEach()用法及代码示例


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 forEach() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。