gtludwig:
Eu tenho um cenário semelhante a:
public class A {
private String id;
@ManyToMany
private Set<B> bSet;
// getters and setters
}
e
public class B {
private String id;
// other attributes
// getters and setters
}
Como posso encontrar um exemplo de A
quando eu tiver uma instância B
usando a stream()
API? Eu estava tentando algo como:
public A findAFromB(B b) {
List<A> aList = aService.findAll();
Optional<A> matchingObject = aList.stream().filter({find a where a.getBSet().contains(b)}).getA();
return (A) matchingObject.get();
}
Como escrever corretamente este filtro?
também:
Soemthing como o uso de um findFirst
ou findAny
como um terminal de operação:
Optional<A> matchingObject = aList.stream()
.filter(a -> a.getbSet().contains(b))
.findFirst();