仅对一个流和过滤器进行两次检查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅对一个流和过滤器进行两次检查相关的知识,希望对你有一定的参考价值。
我需要检查类的Id变量是否为null。如果为null,则必须将其插入一个列表中;如果不为null,则必须将其插入另一列表中。我这样做是:
我不希望有两个流,我只想做一个,而Strem和过滤器,你能帮我吗?
具有空ID的电子邮件将保留在数据库中。
List<Email> emails = payment.getUser().getEmails();
if (!emails.isEmpty())
List<Email> create = emails.stream().filter(email -> email.getId() == null).collect(Collectors.toList());
List<Email> existingEmails = emails.stream().filter(email -> email.getId() != null).collect(Collectors.toList());
答案
使用分区方式
List<Email> emails = payment.getUser().getEmails();
Map<Boolean, List<Email>> result = emails.stream().collect(Collectors.partitioningBy(email -> email.getId() == null));
List<Email> create = result.get(true);
List<Email> existingEmails = result.get(false);
以上是关于仅对一个流和过滤器进行两次检查的主要内容,如果未能解决你的问题,请参考以下文章
[ jquery 过滤器 offsetParent() ] 此方法用于在选择器的基础之上搜索被选元素有定位的父级元素,仅对可见元素有效