|
| 1 | +import java.util.LinkedList; |
| 2 | +import java.util.Queue; |
| 3 | + |
| 4 | +public class QueueSizeSearchFrontExample { |
| 5 | + public static void main(String[] args) { |
| 6 | + Queue<String> waitingQueue = new LinkedList<>(); |
| 7 | + |
| 8 | + waitingQueue.add("Jennifer"); |
| 9 | + waitingQueue.add("Angelina"); |
| 10 | + waitingQueue.add("Johnny"); |
| 11 | + waitingQueue.add("Sachin"); |
| 12 | + |
| 13 | + System.out.println("WaitingQueue : " + waitingQueue); |
| 14 | + |
| 15 | + // Check is a Queue is empty |
| 16 | + System.out.println("is waitingQueue empty? : " + waitingQueue.isEmpty()); |
| 17 | + |
| 18 | + // Find the size of the Queue |
| 19 | + System.out.println("Size of waitingQueue : " + waitingQueue.size()); |
| 20 | + |
| 21 | + // Check if the Queue contains an element |
| 22 | + String name = "Johnny"; |
| 23 | + if(waitingQueue.contains(name)) { |
| 24 | + System.out.println("WaitingQueue contains " + name); |
| 25 | + } else { |
| 26 | + System.out.println("Waiting Queue doesn't contain " + name); |
| 27 | + } |
| 28 | + |
| 29 | + // Get the element at the front of the Queue without removing it using element() |
| 30 | + // The element() method throws NoSuchElementException if the Queue is empty |
| 31 | + String firstPersonInTheWaitingQueue = waitingQueue.element(); |
| 32 | + System.out.println("First Person in the Waiting Queue (element()) : " + firstPersonInTheWaitingQueue); |
| 33 | + |
| 34 | + // Get the element at the front of the Queue without removing it using peek() |
| 35 | + // The peek() method is similar to element() except that it returns null if the Queue is empty |
| 36 | + firstPersonInTheWaitingQueue = waitingQueue.peek(); |
| 37 | + System.out.println("First Person in the Waiting Queue : " + firstPersonInTheWaitingQueue); |
| 38 | + |
| 39 | + } |
| 40 | +} |
0 commit comments