Message queue with multiple producer, single consumer and no duplicates
You code looks like it works just fine, but only because you have a single consumer thread. If you have multiple consumers, then you have a race-condition in your takeMessage method (a different thread may empty the queue after you call hasNext() but before you call next() causing an iteration exception....).

The whole iterator method also feels... clunky, and is a result of the basic Set usage.

I can't help but think that, in your situation, the use of the ConcurrentMap backing the set, is overkill. I know that HashSets have a backing Map as well, but that's different.

So, having thought some more about it as well, I also have to question the need to de-duplicate the producer messages as well in the Set... are you sure you need to?

Also, since you have the system you do, are you sure the queue will ever be big enough to worry about performance of the Set?

I can't help but feel you should go one of two ways:


Just use a LinkedTransferQueue and deal with duplicates elsewhere (use the add and poll methods respectively).
Just use a List with a simple contains() and basic synchronization.


Some other notes....


Why do you have a constructor that takes an initial size, but then does nothing?


Here's a simple synchronized version:

public class MessageClassHolder&lt;T&gt; {

    private final LinkedList&lt;T&gt; messages;

    public MessageClassHolder() {
        messages = new LinkedList&lt;&gt;();
    }

    /**
     * @param message to be inserted
     * @return if message already present returns false
     */
    public boolean insertMessage(T message) {
        synchronized(messages) {
            if (messages.contains(message)) {
                return false;
            }
            return messages.add(message);
        }
    }

    /**
     * @return returns the optional message and removes it
     */
    public Optional&lt;T&gt; takeMessage() {
        synchronized(messages) {
            return messages.isEmpty() ? Optional.empty() : Optional.of(messages.removeFirst());
        }
    }

    /**
     * Clear the message set
     */
    public void clear() {
        synchronized(messages) {
            messages.clear();
        }
    }
}