我正在向用户返回以下响应
class FinalResponseDTO {
List<Service1ResponseDTO> service1ResponseDTO;
Long totalCount;
List<Service2ResponseDTO> service2ResponseDTO;
}
Run Code Online (Sandbox Code Playgroud)
截至目前,我正在进行三个连续调用来计算 this FinalResponseDTO,每个调用都可以独立于其他调用运行。我尝试制作三种不同CompletableFuture的:
CompletableFuture<List<Service1ResponseDTO> future1 = CompletableFuture.supplyAsync(() -> service1.callMethod1());
CompletableFuture<Long> future2 = CompletableFuture.supplyAsync(() -> service2.callMethod2());
CompletableFuture<Service2ResponseDTO> future3 = CompletableFuture.supplyAsync(() -> service3.callMethod3());
Run Code Online (Sandbox Code Playgroud)
如果我这样做CompletableFuture.allOf(future1, future2, future3).join();或我应该打电话吗CompletableFuture.allOf(future1, future2, future3).get();?即使我调用其中任何一个join,或者get我应该如何FinalResponseDTO从中构建。我对 Java 8 并发功能不熟悉,例如,CompletableFuture我很困惑,因为每个 future 的返回类型都不同,我应该如何获得所有这些 future 的组合响应,然后构造我的最终输出?
我有List<Long> countriesList其中包含Long国家/地区 ID 值。
List<UserRequests>现在我正在使用迭代一些列表streams。
userRequests.stream().
forEach(userRequest->
{
UserData=userRepository.findById(userRequest.getId()); //fine
if (CollectionUtils.isNotEmpty(countriesList) && countriesList.contains(userRequest.getCountryId()))//getting NPE here
{
//do some operation
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试通过评估各个语句来进行调试。我已确保countriesList有一些数据第一部分CollectionUtils.isNotEmpty(countriesList)正在返回true。
userRequest也不为 null 但userRequest.getCountryId()为 null。当我评估时countriesList.contains(userRequest.getCountryId()),我就到了Null pointer exception这里。为什么不false?我很困惑我做错了什么。默认list.contains(null)行为只是这样还是因为我在内部调用它stream()?
为了简化起见,我创建了简单的列表并与null.
class Test {
public static void main(String[] args) {
List<Long> longList = List.of(1L, 2L, 3L);
System.out.println("Comparing long list with null::" + longList.contains(null)); …Run Code Online (Sandbox Code Playgroud)