Skip to content

[Term Entry] Java Queue: add() (#7362) #7449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions content/java/concepts/queue/terms/add/add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
Title: '.add()'
Description: 'Inserts an element at the back of the queue.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Collections'
- 'Data Structures'
- 'Methods'
- 'Queues'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

The **`.add()`** method is a built-in method of the [`Queue`](https://www.codecademy.com/resources/docs/java/queue) interface in Java that inserts an element at the back of the `queue`. It returns true if the element is successfully added. Unlike `.offer()`, which returns false if the insertion fails, `.add()` throws an [`Exception`](https://www.codecademy.com/resources/docs/java/errors). This method is commonly used with implementations such as `LinkedList` and `ArrayDeque`.

## Syntax

```pseudo
queue_name.add(E element);
```

**Parameters:**

The `.add()` method requires an `element` as a parameter. The `E` is the type of the `element` parameter, which is the type of elements held in the `queue_name` [collection](https://www.codecademy.com/resources/docs/java/collection).

**Return value:**

The `.add()` method returns `true` upon success. If the queue has a capacity limit and is full, `.add()` throws an `IllegalStateException`.

## Example

In this example, a `Queue` in Java is created using `LinkedList`, elements are added to it, and then the `.add()` method is used to insert an additional element at the end of the queue:

```java
import java.util.*;
class JAVA {
public static void main(String args[]){
Queue<Integer> myQueue = new LinkedList<Integer>();
myQueue.add(5);
myQueue.add(2);
myQueue.add(11);
myQueue.add(7);
myQueue.add(8);

// myQueue before adding 3: 5 => 2 => 11 => 7 => 8
System.out.println("The elements in the queue are: " + myQueue);

// adding 3 to myQueue
boolean success = myQueue.add(3);
System.out.println("3 inserted at the back of the queue: " + success);

// myQueue after adding 3: 5 => 2 => 11 => 7 => 8 => 3
System.out.println("The elements in the queue are: " + myQueue);
}
}
```

The output generated by this code is:

```shell
The elements in the queue are: [5, 2, 11, 7, 8]
3 inserted at the back of the queue: true
The elements in the queue are: [5, 2, 11, 7, 8, 3]
```

Visual representation of the `.add()` method:

![A diagram showing the addition of an item to a queue. The top row displays a queue with the numbers 5, 2, 11, 7, and 8. Below, the code "queue_name.add(3);" is shown, with an arrow pointing to the end of the queue. The bottom row shows the updated queue: 5, 2, 11, 7, 8, 3, illustrating that the number 3 was added to the end of the queue.](https://raw.githubusercontent.com/Codecademy/docs/main/media/java-queue-add.png)
Binary file added media/java-queue-add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.