Online Java Compiler By
JavaTpoint.com
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
queue = new ConcurrentLinkedQueue
(); 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"; } }
Output