Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - Queue remove() Method



Description

The Java Queue remove() method removes first element of the list. Shifts any subsequent elements to the left (subtracts one from their indices).

Declaration

Following is the declaration for java.util.Queue.remove() method

public E remove(int index)

Parameters

NA

Return Value

This method returns the first element.

Exception

NoSuchElementException − if list is empty.

Example 1

The following example shows the usage of Java Queue remove() method. We're creating a Queue of Integers. We're adding couple of Integers to the Queue object using add() method calls per element. Queue size is printed, queue is printed and using remove() method, first element is removed. Then size and queue is printed again.

package com.tutorialspoint;

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
   public static void main(String[] args) {
      
      // create an empty queue
      Queue<Integer> queue = new LinkedList<>();

      // use add() method to add elements in the queue
      queue.add(20);
      queue.add(15);
      queue.add(30);
      queue.add(45);

      System.out.println("Size of list: " + queue.size());
	  // let us print all the elements available in list again
      System.out.println("Queue = " + queue);
      
      // Removes first element 
      queue.remove();

      System.out.println("Now, Size of list: " + queue.size());
      
      // let us print all the elements available in list again
      System.out.println("Queue = " + queue);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 4
Queue = [20, 15, 30, 45]
Now, Size of list: 3
Queue = [15, 30, 45]

Example 2

The following example shows the usage of Java Queue remove(object) method. We're creating a Queue of String. We're adding couple of Strings to the Queue object using add() method calls per element. Queue size is printed, queue is printed and using remove(object) method, an element is removed. Then size and queue is printed again.

package com.tutorialspoint;

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
   public static void main(String[] args) {
      
      // create an empty queue
      Queue<String> queue = new LinkedList<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("B");
      queue.add("C");
      queue.add("D");

      System.out.println("Size of list: " + queue.size());
	  // let us print all the elements available in list again
      System.out.println("Queue = " + queue);
      
      // Removes element B
      queue.remove("B");

      System.out.println("Now, Size of list: " + queue.size());
      
      // let us print all the elements available in list again
      System.out.println("Queue = " + queue);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 4
Queue = [A, B, C, D]
Now, Size of list: 3
Queue = [A, C, D]

Example 3

The following example shows the usage of Java Queue remove(index) method. We're creating a Queue of Student objects. We're adding couple of Students to the Queue object using add() method calls per element. Queue size is printed, queue is printed and using remove(index) method, an element is removed. Then size and queue is printed again.

package com.tutorialspoint;

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
   public static void main(String[] args) {
      
      // create an empty queue
      Queue<Student> queue = new LinkedList<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));

      System.out.println("Size of list: " + queue.size());
	  // let us print all the elements available in list again
      System.out.println("Queue = " + queue);
      
      // Removes element at 3rd position
      queue.remove(2);

      System.out.println("Now, Size of list: " + queue.size());
      
      // let us print all the elements available in list again
      System.out.println("Queue = " + queue);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 3
Queue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Now, Size of list: 2
Queue = [[ 1, Julie ], [ 2, Robert ]]
java_util_queue.htm
Advertisements