🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The PriorityQueue class in Java provides the forEach(Consumer<? super E> action) method to perform the given action for each element in the queue.
Table of Contents
- Introduction
forEachMethod Syntax- Examples
- Using
forEachto Print Elements - Using
forEachto Perform Custom Actions
- Using
- Real-World Use Case
- Use Case: Task Management System
- Conclusion
Introduction
The PriorityQueue.forEach(Consumer<? super E> action) method is used to iterate over each element in the PriorityQueue and perform the specified action. This method is useful for processing all elements in the queue without modifying it.
forEach Method Syntax
The syntax for the forEach method is as follows:
public void forEach(Consumer<? super E> action)
- The method takes a single parameter
actionof typeConsumer<? super E>, which defines the action to be performed on each element. - The method does not return any value.
Examples
Using forEach to Print Elements
The forEach method can be used to print all elements in a PriorityQueue.
Example
import java.util.PriorityQueue;
public class PriorityQueueForEachExample {
public static void main(String[] args) {
// Creating a PriorityQueue of Strings
PriorityQueue<String> tasks = new PriorityQueue<>();
// Adding elements to the PriorityQueue
tasks.add("Complete project report");
tasks.add("Email client updates");
tasks.add("Prepare presentation");
// Using forEach to print all elements
tasks.forEach(task -> System.out.println("Task: " + task));
}
}
Output:
Task: Complete project report
Task: Email client updates
Task: Prepare presentation
Using forEach to Perform Custom Actions
The forEach method can also be used to perform custom actions on each element in the PriorityQueue.
Example
import java.util.PriorityQueue;
public class CustomActionForEachExample {
public static void main(String[] args) {
// Creating a PriorityQueue of Strings
PriorityQueue<String> tasks = new PriorityQueue<>();
// Adding elements to the PriorityQueue
tasks.add("Complete project report");
tasks.add("Email client updates");
tasks.add("Prepare presentation");
// Using forEach to perform custom actions on all elements
tasks.forEach(task -> {
// Custom action: print task in uppercase
System.out.println("Task: " + task.toUpperCase());
});
}
}
Output:
Task: COMPLETE PROJECT REPORT
Task: EMAIL CLIENT UPDATES
Task: PREPARE PRESENTATION
Real-World Use Case
Use Case: Task Management System
In a task management system, you might need to iterate over all tasks in the queue to perform actions such as logging, updating statuses, or sending notifications. The forEach method can help achieve this functionality.
Example
import java.util.PriorityQueue;
public class TaskManagementSystem {
public static void main(String[] args) {
// Creating a PriorityQueue to store tasks
PriorityQueue<Task> tasks = new PriorityQueue<>();
// Adding initial tasks with different priorities
tasks.add(new Task("Complete project report", 2));
tasks.add(new Task("Email client updates", 1));
tasks.add(new Task("Prepare presentation", 3));
// Using forEach to log task details
tasks.forEach(task -> System.out.println("Logging task: " + task));
}
}
class Task implements Comparable<Task> {
private String description;
private int priority;
public Task(String description, int priority) {
this.description = description;
this.priority = priority;
}
@Override
public int compareTo(Task other) {
return Integer.compare(this.priority, other.priority);
}
@Override
public String toString() {
return description + " (Priority: " + priority + ")";
}
}
Output:
Logging task: Email client updates (Priority: 1)
Logging task: Complete project report (Priority: 2)
Logging task: Prepare presentation (Priority: 3)
Conclusion
The PriorityQueue.forEach(Consumer<? super E> action) method in Java is used for performing actions on each element in the queue.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment