What is the need for an Immutable Queue?

Viewed 4864

I have been working with Java for several years now. Recently came across Vavr, a functional library for Java, that provides immutable collection API. I am curious to know the reason for having an immutable Queue.

My understanding is that a Queue is used to produce data to it on one end and then a different thread consumes the data from the other end.

Immutable Queue doesn`t allow you to add data after its construction, then why would I use a Queue here.

Ideally, I would process a Queue as below, but for an immutable Queue, this goes into an infinite loop.

while(!queue.isEmpty()) {
    queue.dequeue(); // process elements in queue.
}

When I googled, all of the discussions are around how to implement an immutable queue, but doesn't explain the need for it.

1 Answers
Related