empty queue java code example

Example 1: java queue

import java.util.*;

Queue<Integer> queue = new LinkedList<Integer>();
// use non-primative types when constructing

// to add a value to the back of queue:
queue.add(7);

// to remove and return front value:
int next = queue.remove();

// to just return front value without removing:
int peek = queue.peek();

Example 2: queue.poll() in java

//prints the head and deletes the head.
q=[10,20,30]
int n=q.poll();//returns 10   q=[20,30]
int m=q.poll();//returns 20   q=[30]

Example 3: how to implements an empty queue java

import java.util.*;
 
public class Main {
   public static void main(String[] args) {
    //declare a Queue   
    Queue<String> str_queue = new LinkedList<>();
    //initialize the queue with values
    str_queue.add("one");
    str_queue.add("two");
    str_queue.add("three");
    str_queue.add("four");
    //print the Queue
    System.out.println("The Queue contents:" + str_queue);
    }
}

Example 4: queue.isempty java

while (!QUEUE.isEmpty()) {
  QUEUE.remove();
 }