ArrayBlockingQueue method forEach() in Java
A ArrayBlockingQueue method forEach() performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
Syntax
 1 default void forEach(Consumer<? super T> action)
action : The action to be performed for each element of this queue.

Exception:

1. NullPointerException : if the specified action is null
ArrayBlockingQueue method forEach()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.ArrayBlockingQueue;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(5);
 11         abq.addAll(Arrays.asList(10, 30, 20, 80, 70));
 12         System.out.println("ArrayBlockingQueue elements : " + abq);
 13 
 14         abq.forEach(ele -> System.out.print(ele + ", "));
 15     }
 16 }
In the above example, a ArrayBlockingQueue of type Integer created and elements added using addAll() method by passing a collection. A forEach() method called by passing a lambda function that prints element separated by comma.
Output
 1 ArrayBlockingQueue elements : [10, 30, 20, 80, 70]
 2 10, 30, 20, 80, 70,

ArrayBlockingQueue method forEach() with String

ArrayBlockingQueue method forEach() with String
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.ArrayBlockingQueue;
 5 import java.util.function.Consumer;
 6 
 7 public class StringExp {
 8 
 9     public static void main(String[] args) {
 10 
 11         ArrayBlockingQueue<String> abq = new ArrayBlockingQueue<>(5);
 12         abq.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
 13         System.out.println("ArrayBlockingQueue elements : " + abq);
 14 
 15         // consumer object by assigning a lambda function
 16         Consumer<String> consumer = str -> System.out.print(str.toUpperCase() + ", ");
 17 
 18         // iterate elements using forEach()
 19         abq.forEach(consumer);
 20     }
 21 }
In the above example, a ArrayBlockingQueue of type String created and elements added using addAll() method by passing a collection. A Consumer object created of type String and assign lambda function that converts string to uppercase and print separated by comma. A forEach() method called by passing a Consumer object which will be executed for each element of this queue.
Output
 1 ArrayBlockingQueue elements : [Welcome, to, IOGyan]
 2 WELCOME, TO, IOGYAN,

ArrayBlockingQueue method forEach() with custom object

ArrayBlockingQueue method forEach() with custom object
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.concurrent.ArrayBlockingQueue;
 6 import java.util.function.Consumer;
 7 
 8 public class Example {
 9 
 10     public static void main(String[] args)  {
 11 
 12         Employee emp1 = new Employee(100, "Emp1", "Tech",  12000);
 13         Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
 14         Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
 15 
 16         List<Employee> list = Arrays.asList(emp1, emp3, emp2);
 17         ArrayBlockingQueue<Employee> abq = new ArrayBlockingQueue<>(5);
 18         abq.addAll(list);
 19         System.out.println("ArrayBlockingQueue elements : " + abq);
 20 
 21         // consumer object by assigning a lambda function
 22         Consumer<Employee> consumer = emp -> {
 23             System.out.println(emp.getName() + " -> " + emp.getSalary());
 24         };
 25 
 26         // iterate elements using forEach()
 27         abq.forEach(consumer);
 28     }
 29 }
In the above example, a ArrayBlockingQueue of type Employee created and elements added using addAll() method by passing a collection. A Consumer object created of type Employee and assign lambda function that prints employee name and salary. A forEach() method called by passing a Consumer object which will be executed for each element of this queue.
Output
 1 ArrayBlockingQueue elements : [Emp1 Tech 12000, Emp3 Admin 21000, Emp2 Tech 31000]
 2 Emp1 -> 12000
 3 Emp3 -> 21000
 4 Emp2 -> 31000

Employee class

Employee class
 1 package com.java;
 2 
 3 public class Employee implements Comparable<Employee> {
 4 
 5     private final int id;
 6     private final String name;
 7     private final String dept;
 8     private final Long salary;
 9 
 10     public Employee(int id, String name, String dept, long salary) {
 11         this.id = id;
 12         this.name = name;
 13         this.dept = dept;
 14         this.salary = salary;
 15     }
 16     public int getId() {
 17         return this.id;
 18     }
 19 
 20     public String getDept() {
 21         return this.dept;
 22     }
 23 
 24     public String getName() {
 25         return this.name;
 26     }
 27 
 28     public Long getSalary() {
 29         return this.salary;
 30     }
 31 
 32     @Override
 33     public int hashCode() {
 34         return this.getId() + this.getName().hashCode();
 35     }
 36 
 37     @Override
 38     public boolean equals(Object obj) {
 39         return this.id == ((Employee)obj).id;
 40     }
 41 
 42     public String toString() {
 43         return this.name + " " + this.dept + " " + this.salary;
 44     }
 45 
 46     @Override
 47     public int compareTo(Employee o) {
 48         return this.getId() - o.getId();
 49     }
 50 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us