elect elect - 1 year ago 207
Java Question

Java stream, remove and perform action from ConcurrentLinkedQueue

I am unsure how to do this, I'd like to iterate the

(all of it), removing the i-th item and performing some code on it.

This is what I was used to do:

public static class Input {

public static final ConcurrentLinkedQueue<TreeNode> treeNodes = new ConcurrentLinkedQueue<>();

public static class Current {

public static final ConcurrentHashMap<Integer, TreeNode> treeNodes = new ConcurrentHashMap<>();

is a simple class

TreeNode treeNode = Input.treeNodes.poll();

while (treeNode != null) {


Current.treeNodes.put(treeNode.getId(), treeNode);

treeNode = Input.treeNodes.poll();

This is how I am trying to do using stream:

.forEach(treeNode -> {
Current.treeNodes.put(treeNode.getId(), treeNode);

I am afraid that something may be error prone having to remove the item inside the

So my question is:

Is this safe and/or are there any better ways to do it?

Answer Source

Just as you've assumed, you should not modify the backing collection while processing the stream because you might get a ConcurrentModificationException (just as with for(Object o:objectArray){} loops)

On the other hand it is not very clear which TreeNode you are trying to remove, as in the current case, seemingly you wish to remove all elements from the List, perform some actions on them and put them in a Map.

You may safely achieve your current logic via:

         .map(treeNode -> {
             Current.treeNodes.put(treeNode.getId(), treeNode);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download