我有两个列表,一个是messagePermissionResponseDTOList
,另一个是dispatchSMSQList
。
我想从dispatchSMSQList
中取出被阻止的号码列表。以下是我的工作代码段。
您能指导一下如何转换Lambda表达式或其正确形式吗?
测试数据的有效链接为https://repl.it/repls/FriendlyImmenseClasses
TreeSet<Long> blockedNumbersSet = new TreeSet<>();
for (MessagePermissionResponseDTO permission: messagePermissionResponseDTOList) {
if (permission.isBlocked()) {
blockedNumbersSet.add(permission.getPhoneNumber());
}
}
List<DispatchSMSQ> blockedNumbers = dispatchSMSQList.stream().filter(t -> blockedNumbersSet.contains(t.getMdn())).collect(Collectors.toList());
System.out.print("blockedNumbers-->"+ blockedNumbers.size());
答案 0 :(得分:1)
您可以这样做
List<DispatchSMSQ> blockedNumbers = messagePermissionResponseDTOList.stream()
.filter(MessagePermissionResponseDTO::isBlocked)
.map(MessagePermissionResponseDTO::getPhoneNumber)
.collect(Collectors.collectingAndThen(Collectors.toSet(),
s -> dispatchSMSQList.stream()
.filter(d -> s.contains(d.getMdn())).collect(Collectors.toList())));
答案 1 :(得分:0)
这应该做到
messagePermissionResponseDTOList
.stream()
.filter(MessagePermissionResponseDTO::isBlocked)
.map(MessagePermissionResponseDTO::getPhoneNumber)
.collect(Collectors.toSet())