-1

Actually I want to throw two exceptions as part of checking two conditions. But wondering how to throw these exceptions after streaming and mapping it.

This is the code to be converted to Java-8 Using Streams

  for(GroupCallCenter existingGroupCall : group.getGroupCallCenters())
   {
        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());   
       }
   }
Jeeva D
  • 304
  • 1
  • 4
  • 15

1 Answers1

1

You can just apply the simple stream forEach like below:

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());   
                }
            });

You have not mention what is accountCallCenterResource object in your code. You have to make sure that accountCallCenterResource is final or effectivaly final to use it inside the stream method.

For more detailed understanding you can refer this

Deepak Kumar
  • 1,246
  • 14
  • 38
  • Why not `group.getGroupCallCenters().forEach()` directly? What benefit it provides? The [tag:java-stream] should **not** be used like this. – Nikolas Charalambidis Apr 15 '19 at 13:25
  • @Nikolas, We can even use the `group.getGroupCallCenters().forEach()`. But He wanted to use it using stream. SO i kept it like this – Deepak Kumar Apr 15 '19 at 13:27
  • 1
    Again, what is the benefit? You might want to read [What is difference between Collection.stream().forEach() and Collection.forEach()?](https://stackoverflow.com/questions/23218874/what-is-difference-between-collection-stream-foreach-and-collection-foreach) to get proved why your approach violates the [tag:java-stream] principles. – Nikolas Charalambidis Apr 15 '19 at 13:29
  • I don’t understand your rage - do you have a reason to downvote my [question](https://stackoverflow.com/questions/52272533/java-8-stream-api-does-any-stateful-intermediate-operation-guarantee-a-new-sou) because you don’t disagree with my comment? – Nikolas Charalambidis Apr 15 '19 at 21:08