我有一个类似的场景:
public class A {
private String id;
@ManyToMany
private Set<B> bSet;
// getters and setters
}
and
public class B {
private String id;
// other attributes
// getters and setters
}
我怎样才能找到一个实例A
当我有一个实例时B
使用stream()
API?我正在尝试类似的事情:
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();
}
如何正确编写这个过滤器?
类似使用findFirst
or findAny
作为终端操作:
Optional<A> matchingObject = aList.stream()
.filter(a -> a.getbSet().contains(b))
.findFirst();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)