gtludwig:
Tengo un escenario similar a:
public class A {
private String id;
@ManyToMany
private Set<B> bSet;
// getters and setters
}
y
public class B {
private String id;
// other attributes
// getters and setters
}
¿Cómo puedo encontrar un ejemplo de A
cuando tengo un caso de B
uso de la stream()
API? Estaba tratando 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();
}
Cómo escribir correctamente este filtro?
también:
Soemthing como usar una findFirst
o findAny
como una operación de terminal:
Optional<A> matchingObject = aList.stream()
.filter(a -> a.getbSet().contains(b))
.findFirst();