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


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


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