Javatpoint Logo
Javatpoint Logo

Various Operations on Queue in Java

In computer science, queues are a basic data structure that are frequently used, especially in programming. It is a collection of items that are added and taken out in a particular sequence called the first-in, first-out (FIFO) order. Queues can be implemented in a variety of programming languages, including Java, and they offer a number of operations necessary for manipulating the data they hold. The numerous actions that can be performed on a queue in Java will be covered in this article, along with how to implement them using the built-in Queue interface and its implementing classes.

Enqueue:

The enqueue process inserts an element at the end of the queue. The offer() method in Java is utilised to insert an element into the queue. This method returns a boolean value indicating whether or not the element was properly added to the queue. In the event that the queue has a fixed size and is complete, the offer() method returns false.

Example:

Dequeue:

The dequeue process allows an element to be taken out of the front of the queue. To remove the head part of a queue in Java, use the poll() method. When the queue is empty, this method returns null.

Example:

Peek:

The element at the front of the queue is retrieved using the peek operation without being taken out of the queue. The head member of the queue is retrieved using Java's peek() method. This method gives null when the queue is empty.

Example:

Size:

Use the size procedure to calculate the queue's element count. To determine how many items are in the queue, use Java's size() method.

Example:

Empty:

The empty procedure is used to determine whether or not the queue is empty. The isEmpty() function in Java is used to determine whether or not the queue is empty. If the queue is empty, this method gives true; otherwise, it returns false.

Example:

Search:

In order to find an element in the queue, use the search procedure. When looking for a queue member in Java, one uses the indexOf() method. Depending on whether the element was discovered or not, this method will either return -1 or the index of the element's first occurrence in the queue.

Example:

Complete Code

QueueExample.java

Output:

Removed Element: 10
Peeked Element: 20
Size of Queue: 2
Is Queue Empty: false
Index of Element 20: 0

In the above code, all the actions covered in the previous article are used to build a queue using the LinkedList class. The offer() method is used to add elements to the queue, the poll() method is used to remove the first element, the peek() method is used to retrieve the first element without removing it, the size() method is used to calculate the queue's size, the isEmpty() method is used to determine whether the queue is empty, and the indexOf() method is used to find a specific element in the queue. The results of each action carried out on the queue are displayed in the programme's output.

Conclusion

In conclusion, queues are a crucial type of data structure in Java and provide a number of methods for working with the data they hold. A straightforward method for creating queues in Java is through the built-in Queue interface and its implementing classes. Enqueue, dequeue, peek, size, empty, and search are all essential operations for working with queues and can be used to effectively handle data in a variety of applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA