Javatpoint Logo
Javatpoint Logo

Generic Queue Java

In computer programming, a queue is a fundamental data structure that stores items in a linear order, adhering to the First-In-First-Out (FIFO) principle. It implies that the first element to be eliminated will be the one that was added first. Applications like work scheduling, event management, and data processing frequently employ queues. The Java Collections Framework in Java has the Queue interface along with a number of other classes and interfaces for working with collections of objects. The ability to employ generics, which enables programmers to design queues that may store components of any data type, is one of the primary characteristics of Java's Queue interface.

Generic Queue Implementation in Java

The Java Queue interface extends the Collection interface and is a component of the java.util package. It specifies a number of ways to insert, delete, and modify elements in a queue. Here is how the Java Queue interface is declared:

As seen from the above code snippet, the Queue interface is defined using a type parameter <E>, which represents the type of elements that can be stored in the queue. This allows developers to create a generic queue that can store elements of any data type, such as integers, strings, or custom objects.

Let's take a look at some of the methods provided by the Queue interface:

add(E e) and offer(E e): Adding an element to the end of the queue is accomplished using these techniques. When an element can't be added to the queue, the add() method throws an error; in contrast, the offer() method returns false.

remove() and poll(): The element can be taken out of the queue's front and returned using these techniques. While the poll() method will return null if the queue is empty, the delete() method will throw an exception if it encounters this situation.

element() and peek(): Without taking the element out of the queue, these techniques are utilised to retrieve it from the front. When the queue is empty, the element() method throws an exception while the peek() method returns null.

Example:

Let's look at some example programs that demonstrate the usage of a generic queue in Java.

Example 1: Integer Queue

IntegerQueueExample.java

Output:

Elements in the queue: [10, 20, 30]
Front element of the queue: 10
Elements in the queue after removal: [20, 30]

In this example, we use the LinkedList class's implementation of the Queue interface to establish a queue to hold numbers. Using the add() method, we add three numbers to the queue. Next, we use the LinkedList class's toString() method to show the components of the queue. The front element of the queue is then removed and displayed using the remove() function, and then the remaining queue elements are displayed using the toString() method once more.

Example 2: String Queue

StringQueueExample.java

Output:

Elements in the queue: [apple, banana, cherry]
Front element of the queue: apple
Elements in the queue after removal: [banana, cherry]

In this example, we use the LinkedList class's implementation of the Queue interface to establish a queue to hold strings. Utilising the offer() method, we add three strings to the queue before displaying their contents. We then use the poll() method to remove and display the queue's front element before displaying the queue's remaining elements.

Example 3: Custom Object Queue

CustomObjectQueueExample.java

Output:

Elements in the queue: [Student [name=John, age=20], Student [name=Alice, age=22], Student [name=Bob, age=21]]
Front element of the queue: Student [name=John, age=20]
Elements in the queue after removal: [Student [name=Alice, age=22], Student [name=Bob, age=21]]

In this example, we create a queue to store custom objects of the Student class. We define a Student class with two attributes, name and age, and override the toString() method to provide a custom string representation of the Student object. We create three Student objects and add them to the queue using the add() method. Then we display the elements in the queue using the toString() method. Next, we remove and display the front element of the queue using the poll() method, and finally, we display the elements in the queue after removal.

In addition to that about generic queues we have:

Capacity Restrictions: Generic queues generated via the Queue interface and the LinkedList class don't have a fixed capacity, in contrast to Java arrays, which have a predetermined size. Depending on how many components are added to or withdrawn from the queue, they may dynamically expand or contract in size. As a result, there is more freedom in how queue items are managed as there is no need to set an initial size or be concerned about filling the queue to capacity.

Null Elements: Java's generic queues allow the presence of null entries. As a result, null can be added to a queue as a legitimate element and will be handled the same as any other element. However, it's important to be cautious when using null elements in a queue, as they can sometimes cause unexpected behavior in your code if not handled properly.

Queue Implementations: Java also offers the ArrayDeque and PriorityQueue classes, which can be used to construct general queues in addition to the LinkedList class. Depending on the particular use case, these classes have a variety of traits and performance trade-offs. For instance, PriorityQueue is a priority-based queue that sorts objects according to their natural order or a custom comparator, while ArrayDeque is a double-ended queue that may be used as both a queue and a stack.

In this section, we have discussed the concept of a generic queue in Java, which is a commonly used data structure that follows the FIFO (First-In, First-Out) principle. We have explored the basic operations of a queue, including adding elements to the back of the queue, removing elements from the front of the queue, and peeking at the front element without removing it. We have also discussed how to implement a generic queue in Java using the Queue interface, which provides a standard set of methods for working with queues, and how to use the LinkedList class as an implementation of the Queue interface. We have also provided example programs with output to demonstrate the usage of generic queues in Java, including integer queues, string queues, and custom object queues.







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