Can Java 8 Streams operate on an item in a collection, and then remove it?

前端 未结 8 1323
不知归路
不知归路 2020-12-23 14:22

Like just about everyone, I\'m still learning the intricacies (and loving them) of the new Java 8 Streams API. I have a question concerning usage of streams. I\'ll provide a

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 14:59

    There are many approaches. If you use myList.remove(element) you must override equals(). What I prefer is:

    allList.removeIf(item -> item.getId().equals(elementToDelete.getId()));
    

    Good luck and happy coding :)

提交回复
热议问题