- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement size-limited Queue that holds last N elements in Java?
Introduction
A queue is an interface in Java. It is used to insert elements at one end and remove them from another end. It uses the FIFO principle for its processing. The queue extends the Collection framework and is defined in the Java.util interface.
In this tutorial, we will understand the implementation of size limited queue in Java.
What is Size Limited Queue in Java?
A size-limited queue is a queue with a fixed size of N. It cannot hold elements more than its size. If you try to push more data, it will remove elements from its front end. The Queue is fixed with its size N.
Size Limited Queue is implemented using the Linked List class and has all the basic methods of simple Queue in Java.
Implementing Size Limited Queue that holds Last N element in java
We are implementing a method to add elements in the size-limited queue till the last size of the Queue by overriding the add() method of the Queue.
Syntax of Size Limited Queue
Queue<data_type> queue_name = new SizeLimitedQueue<>()
Syntax of methods used in the code
add()= queue_name.add()
size()= queue_name.size()
peek()= queue_name.peek()
Algorithm
Step 1: Initialize a queue by extending the Linked list class
Step 2: Declare a variable to check the size of the queue.
Step 3: Override the add() method: when a queue reaches its maximum size, add a new element by removing the front element.
Step 4: print the Queue.
Example
import java.util.LinkedList; import java.util.Queue; public class SVQ { //size limited queue extends the Linked List class public static class SizeLimitedQueue<E> extends LinkedList<E> { //declaring a variable of size limited queue to check the queue size private int QueueVariable; //initializing the constructor method public SizeLimitedQueue(int QueueVariable) { this.QueueVariable = QueueVariable; } //overriding the add() method of Queue, so that Queue adds elements to the QueueVariable else remove the front element and add a new element. @Override public boolean add(E o) { //remove front element, if Queue reached its maximum size while (this.size() == QueueVariable) { super.remove(); } super.add(o); return true; } } public static void main(String[] args) { //initializing a Size limited queue of size 4 Queue<Integer> sq = new SizeLimitedQueue<>(4); //adding elements {0, 1, 2, 3, 4, 5} to the queue for (int x = 0; x < 6; x++) sq.add(x); //Printing size of the Queue int size = sq.size(); System.out.println("Size of queue-" + size); //Printing Queue elements and Queue has {2, 3, 4, 5} and {0, 1} are removed due to size of the queue System.out.println("Elements of queue " + sq); //removing queue front element int h = sq.remove(); System.out.println("Removed element-" + h); System.out.println("Elements of queue " + sq); // print head of the Queue int hq = sq.peek(); System.out.println("Head of queue-" + hq); //adding 6,7 to the queue for (int x = 6; x < 8; x++) sq.add(x); System.out.println("Elements of queue " + sq); } }
Output
Size of queue-4 Elements of Queue [2, 3, 4, 5] Removed element-2 Elements of queue [3, 4, 5] Head of queue-3 Element of queue [4, 5, 6, 7]
Explanation
In the above code −
Extended a Linked list class to implement a size limited queue pq of size 4.
Define a variable QueueVariable to control the size of the defined queue.
Override the add method so that only 4 elements are added at a time in queue pq. The queue will remove the front element if a new element is added.
Adding 0, 1, 2, 3, 4, 5 to the queue. pq will store only 4 elements while removing the front elements. Resulting Queue elements with [2, 3, 4, 5].
Removing the front element 2 from the queue. Now, the queue (pq) has only 3 elements [3, 4, 5], it has a place for 1 new data.
Again, add elements 6 and 7 to the Queue.
Queue pq has space of only one, so to add both 6 and 7, pq will remove the head element [3] and store [4, 5, 6, 7]
Conclusion
We implemented a method to define a size-limited queue of size N in java. I hope this tutorial is meant for you.
To Continue Learning Please Login
Login with Google