0

I have PrimaryDTO class which contains a List<SecondaryDTO> and this SecondaryDTO itself contains a Set<SecondaryCategoryDTO> and finally this SecondaryCategoryDTO contains a boolean status.

In the below statement, I want to check if any status is true then go inside if statement else don't enter.

if(Optional.ofNullable(PrimaryDTO.getSecondaryDTOs()).
    orElse(PrimaryDTO.getSecondaryDTOs()).stream().filter(Objects::nonNull)
    .map(x->x.getSecondaryCategories()).anyMatch(z-> z.isStatus()==true))

But, in the above syntax, z.isStatus() is giving error as it is saying z to be a set. I am already iterating Set via .map with getSecondaryCategories(). I am not getting why it is taking it as set?

2
  • 1
    What’s the purpose of Optional.ofNullable(PrimaryDTO.getSecondaryDTOs()). orElse(PrimaryDTO.getSecondaryDTOs())? Doesn’t it always evaluate to PrimaryDTO.getSecondaryDTOs(), no matter whether it is null? Besides that, you are not iterating the Set, you are just mapping to it. Perhaps, you meant .flatMap(x -> x.getSecondaryCategories().stream()) Commented Oct 19, 2018 at 14:56
  • Thanks @Holger for the comment on Optional, Its a mistake. I didn't got on how to execute the statement if not null since if I use ifPresent, then I cannot throw any Exception from inside it. It asks for try catch block. Commented Oct 19, 2018 at 15:13

1 Answer 1

1

It seems like you might have PrimaryDTO as a null and it's List<SecondaryDTO> as a null, and some elements in that List might be null also, in such a case:

Optional<Boolean> op = Optional.ofNullable(primaryDTO)
            .flatMap(x -> Optional.ofNullable(x.getSecondaryDTOs()))
            .map(x -> x.stream()
                    .filter(Objects::nonNull)
                    .flatMap(y -> y.getSecondaryCategoryDTOs().stream())
                    .map(SecondaryCategoryDTO::isStatus)
                    .anyMatch(z -> z == true)); 

if(op.isPresent()){
    // do your work
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Eugene and Holger for the solution. Its working now. I tried with flatmap but in an incorrect way.
@harshit if you want everything inside the if condition if(Optional.ofNullable(primaryDTO) .flatMap(x -> Optional.ofNullable(x.getSecondaryDTOs())) .filter(x -> x.stream() .filter(Objects::nonNull) .flatMap(y -> y.getSecondaryCategories().stream()) .anyMatch(SecondaryCategoryDTO::isStatus)).isPresent()){...};
.map(SecondaryCategoryDTO::isStatus) .anyMatch(z -> z == true) could be simplified to .anyMatch(SecondaryCategoryDTO::isStatus)
@Aomine this is how I wanted to do initially, but since the OP seems to be confused about Optional in general, i wanted to be as verbose as I could here...
Thanks @Aomine for the alternative solution. It is much helpful. Eugene, Thanks for the simplified solution as it was easy to understand the thing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.