Java 8 stream - como encontrar entidade pai da criança?

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 Aquando eu tiver uma instância Busando 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 findFirstou findAnycomo um terminal de operação:

Optional<A> matchingObject = aList.stream()
        .filter(a -> a.getbSet().contains(b))
        .findFirst();

Acho que você gosta

Origin http://43.154.161.224:23101/article/api/json?id=225625&siteId=1
Recomendado
Clasificación