0

I need assistance with removing the element when the id is equal to a specific constant that I have. However when i run this code it does not remove the element with that constant. I am fairly new to java so I am a bit confused on why this does not remove the elemtn when id is equal to my constant. Any help would be greatly appreciated.

if (CollectionUtils.isNotEmpty(myResponse.getOrders())) {
    List<Order> myList = new ArrayList<>();
    myResponse.getOrders().stream()
            .forEach(myResponse -> {
                if (myResponse.getId() == Action.MY_CONSTANT) {
                    myList.remove(myResponse);
                } else {
                    myList.add(setMyParams(myResponse));
                }
            });
    return myList;
}

I have tried the remove() method inside of my conditional if statement but doesn't work.

1 Answers1

0

You can't modify a collection while you're streaming over it. Start with myList.removeIf(...) and don't add elements back in during the removeIf.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413