Java8流式操作-根据集合的某个属性值取交集
这里是做了两个封装类的集合做了一次筛选,两个封装类的code值作比较
public class Test {
public static void main(String[] args) {
List<XzqhAndJJzz> xzqhAndJjzzeList = new ArrayList<>();
//获取所有建行拉白经济组织
XzqhAndJJzz xzqhAndJJzz1 = new XzqhAndJJzz();
xzqhAndJJzz1.setCode("1001");
XzqhAndJJzz xzqhAndJJzz2 = new XzqhAndJJzz();
xzqhAndJJzz2.setCode("1002");
xzqhAndJjzzeList.add(xzqhAndJJzz1);
xzqhAndJjzzeList.add(xzqhAndJJzz2);
List<YnzlCcbSecurityJjzz> ynzlCcbSecurityJjzzList = new ArrayList<>();
//过滤用户关联经济组织与建行拉白经济组织中的交集
YnzlCcbSecurityJjzz ynzlCcbSecurityJjzz1 = new YnzlCcbSecurityJjzz();
ynzlCcbSecurityJjzz1.setJjzzCode("1003");
YnzlCcbSecurityJjzz ynzlCcbSecurityJjzz2 = new YnzlCcbSecurityJjzz();
ynzlCcbSecurityJjzz2.setJjzzCode("1004");
ynzlCcbSecurityJjzzList.add(ynzlCcbSecurityJjzz1);
ynzlCcbSecurityJjzzList.add(ynzlCcbSecurityJjzz2);
//交集
List<XzqhAndJJzz> newList = xzqhAndJjzzeList
.stream()
.filter(m -> ynzlCcbSecurityJjzzList.stream().map(d -> d.getJjzzCode()).collect(Collectors.toList()).contains(m.getCode()))
.collect(Collectors.toList());
System.out.println(newList.size());
}
}