Given a list of elements, I want to get the element with a given property and remove it from the list. The best solution I found is:
ProducerDTO p = producersProcedureActive
.stream()
.filter(producer -> producer.getPod().equals(pod))
.findFirst()
.get();
producersProcedureActive.remove(p);
Is it possible to combine get and remove in a lambda expression?
get()here! You have no idea whether its empty or not. You'll throw an exception if the element was not there. Instead, use one of the safe methods like ifPresent, orElse, orElseGet, or orElseThrow.listfor which thePredicateis true or only the first (of a possibly zero, one or many elements)?