Size-limited queue that holds last N elements in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Java, a Size-limited Queue, which holds the last N elements, is a useful data structure that provides a way to maintain a collection of elements with a fixed size. As new elements are added beyond the capacity, the oldest elements are automatically evicted, which is particularly useful in scenarios where only the most recent data is needed. This data structure can be akin to a 'sliding window' over a stream of incoming data.
Implementing a Size-limited Queue in Java
In Java, this can be efficiently implemented using the LinkedList or the ArrayDeque classes from the java.util package. Both provide operations that permit adding and removing elements in constant time .
Example Implementation
Here's an example of how to implement a Size-limited Queue using a LinkedList.
Explanation
- Data Structure: We use
LinkedListas the underlying data structure for the queue. This choice allows for efficient insertions and deletions. - Constructor: The constructor takes a single argument that specifies the maximum size of the queue.
- Add method: This method adds a new element to the queue. If the current size of the queue is equal to the maximum allowed size, it removes the oldest element before adding the new one.
- Remove method: Removes and returns the earliest added element, or
nullif the queue is empty. - Peek method: Retrieves, but does not remove, the head of this queue, or returns
nullif the queue is empty. - Size and Empty methods: These provide utility functions to check the current size and whether the queue is empty.
Key Considerations
- Data Loss: The queue's size limit means that the oldest data can be lost. Thus, it's not suitable for scenarios where complete data retention is required.
- Thread Safety: The current implementation is not thread-safe. For multithreading scenarios, consider wrapping the operations in synchronized blocks or using concurrent collections like
BlockingQueuesubclasses. - Use Cases:
- Stream Processing: Useful in event-driven architectures or monitoring systems where only the most recent set of events is important.
- Caching: When coupled with an eviction policy, it could be useful for implementing simple caching mechanisms that automatically remove old data.
- Rate-limiting and Sliding Window Algorithms: Can be used in algorithms that require maintaining a subset of the latest entries.
Performance and Complexity
| Operation | Time Complexity |
| Add | (average case) |
| Remove | |
| Peek | |
| Size and isEmpty |
Conclusion
The Size-limited Queue is a powerful tool in Java for controlling memory usage by maintaining a fixed-size collection of the most recent data. Implementing this using traditional collections like LinkedList or ArrayDeque allows for lean and efficient operations. However, developers should be mindful of the implications regarding data loss and consider thread safety when using this structure in concurrent environments.
Related reading
- skew matrix algorithm
- Skip List vs. Binary Search Tree
- Skip lists, are they really performing as good as Pugh paper claim?
- Sklearn list of algorithms
- Slicing a dictionary
- Slow Performance with Apache Spark Gradient Boosted Tree training runs
- SLF4J Failed to load class org.slf4j.impl.StaticLoggerBinder
- Something like 'contains any' for Java set?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.