1

I have 2 List list:

//Dynamic lists - sent in request
List<String> types = {"VCX", "ATCH", "Warrnty"};

//hardcoded eligble list in code
List<String> eligibleTypes = {"VCX", "ATCH", "Warrnty"};

Now I want to return true if any of the entries in types list is present in hardcoded eligibleTypes list? How can I write a clean code using Java streams?

boolean isAnyChildEligible = types.stream()
    .anyMatch(type -> isEligibleProgram(type, eligibleTypes));

private boolean isEligibleProgram(String type, List<String> eligibleTypes) {
    if(!CollectionUtils.isEmpty(eligibleTypes) && eligibleTypes.contains(type)) {
        return true;
    }
    return false;
}

Any better way of doing the above? Using some other method? Will the above even work?

1
  • which CollectionUtils you use here !CollectionUtils.isEmpty(eligibleTypes)? Commented Apr 20, 2020 at 8:56

2 Answers 2

1

you can use the Collections method like below

Collections.disjoint(types, eligibleTypes)

returns true if no common element between list.

Hope this helps !!!

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming eligibleTypes can never be null, you can write this without the isEligibleProgram() method:

boolean isAnyChildEligible = types.stream().anyMatch(eligibleTypes::contains);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.