/** * @author lautturi.com * Java example: java queue using LinkedList */ import java.util.*; public class Lautturi { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<Integer>(); // add a value to the back of queue: queue.add(1); queue.add(2); queue.add(8); queue.add(5); queue.add(7); System.out.println("The queue:"); // print the Queue // Method 1 - print the Queue in java queue.forEach(e -> System.out.println(e)); // Method 2 - print the Queue in java System.out.println(queue.toString()); // Method 3 - print the Queue in java System.out.println(Arrays.toString(queue.toArray())); // remove and return front value from Queue: int next = queue.remove(); System.out.println("next:" + next); // return front value without removing from Queue: int peek = queue.peek(); System.out.println("peek:" + peek); System.out.println("The queue:"); // print the Queue queue.forEach(e -> System.out.println(e)); } }
output:
The queue: 1 2 8 5 7 [1, 2, 8, 5, 7] [1, 2, 8, 5, 7] next:1 peek:2 The queue: 2 8 5 7