ConcurrentLinkedQueue to unmodifiable list I have a ConcurrentLinkedQueue where I put some mails in. Now I have a visual page where I want to see what's in the Queue, but of course I do not permit that they alter the Queue in any way.

I put a getter, but instead of the Queue, I return a List. We know that the mail does disappear while we are watching, the use case permits that a refresh of the screen is enough to see the new status.

Code at the moment:

private final ConcurrentLinkedQueue&lt;MimeMessage&gt; mailsToSend = new ConcurrentLinkedQueue&lt;MimeMessage&gt;();

public List&lt;MimeMessage&gt; getMailsToSend() {
    return Collections.unmodifiableList(Lists.newArrayList(mailsToSend.iterator()));
}


I know, it's a very small code fragment and much review isn't there, but if there are better ways to convert it to an unmodifiable list, I'd like to hear it.
Lists.newArrayList will allocate a new list (as the name indicates) so there is no reason to wrap it in a Collections.unmodifiableList.

What you have is a defensive copy. This won't allow propagation of changes back to the queue, but requires you to get a new list each time you want an update.

If you still want a queue backed view then you should return Collections.unmodifiableCollection:

public Collection&lt;MimeMessage&gt; getMailsToSend() {
    return Collections.unmodifiableCollection(mailsToSend);
}


This has the downside that only way to get all items from a generic Collection is using the iterator or the toArray methods. Though each time you call it on them you get an up to date view (with the same caveats of the same methods of the queue).