我们可以通过流和映射抛出两个异常吗?

时间:2019-04-15 13:12:25

标签: java java-8

实际上,我想抛出两个异常作为检查两个条件的一部分。但是想知道如何在流式传输和映射之后引发这些异常。

这是使用Streams转换为Java-8的代码

.sdf

1 个答案:

答案 0 :(得分:1)

您只需将简单的流应用于每个,如下所示:

group.getGroupCallCenters().stream().forEach((existingGroupCall) ->
           {
               if(!existingGroupCall.getCallCenter()
              .getId().equals(accountCallCenterResource.getCallCenterId())) 
             {
               if 
              (!accountCallCenterResource.getValidity().getEffectiveStarting()
                    .isAfter(existingGroupCall.getExpirationDate())
                    && !existingGroupCall.getEffectiveDate()                      

       .isAfter(accountCallCenterResource.getValidity().getExpiresAfter())) 
                   {
               throw new ApiException(ApiErrorCode.DEFAULT_400, "Group call center already exist for that period");
                      }
           }
            else {
                throw new DuplicateException(ApiErrorCode.DUPLICATE, 
          existingGroupCall.getId());   
                }
            });

您没有在代码中提及accountCallCenterResource对象。您必须确保accountCallCenterResource是final或有效的final,才能在stream方法中使用它。

要获得更详细的了解,您可以参考this

相关问题