The Java Queue add(E e) method inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
DeclarationFollowing is the declaration for java.util.Queue.add() method
public boolean add(E e)Parameters
e − The element to be appended to this list.
Return ValueThis method returns true.
ExceptionIllegalStateException − if the element cannot be added at this time due to capacity restrictions
ClassCastException − if the class of the specified element prevents it from being added to this queue
NullPointerException − if the specified element is null and this queue does not permit null elements
IllegalArgumentException − if some property of this element prevents it from being added to this queue
Example 1The following example shows the usage of Java Queue add(E) method to add Integers. We're adding couple of Integers to the LinkedList object using add() method calls per element and then print each element to show the elements added.
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(30); queue.add(20); queue.add(30); queue.add(15); queue.add(22); queue.add(11); // let us print all the elements available in queue for (Integer number : queue) { System.out.println("Number = " + number); } } }Output
Let us compile and run the above program, this will produce the following result −
Number = 20 Number = 30 Number = 20 Number = 30 Number = 15 Number = 22 Number = 11Example 2
The following example shows the usage of Java Queue add(E) method to add Strings. We're adding couple of strings to the LinkedList object using add() method calls per element and then printing the Queue using its toString() method.
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("Welcome"); queue.add("To"); queue.add("Tutorialspoint"); System.out.println("Queue = " + queue); } }Output
Let us compile and run the above program, this will produce the following result −
Queue = [Welcome, To, Tutorialspoint]Example 3
The following example shows the usage of Java Queue add(index, E) method to add Student objects at particular index. We're adding couple of Student objects to the LinkedList object using add() method calls per element and using add(index, E) in the end to add a student at particular location and then printing the LinkedList using its toString() method.
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")); 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 + " ]"; } }Output
Let us compile and run the above program, this will produce the following result −
Queue = [[ 1, Julie ], [ 2, Robert ]]
java_util_queue.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4