1

I wish to create a method equivalent to the following using Java 8 streams but not able to do this. Can someone guide me here?

public boolean checkCondition(List<String> ruleValues, List<String> inputValues) {
    boolean matchFound = false;
    for (String ruleValue : ruleValues) {
        for (String inputValue : inputValues) {
            if (ruleValue.equalsIgnoreCase(inputValue)) {
                matchFound = true;
                break;
            }
        }
    }
    return matchFound;
}
khelwood
  • 52,115
  • 13
  • 74
  • 94

2 Answers2

1

Try this approach. It will run in O(n) time:

public boolean checkCondition(List<String> ruleValues, List<String> inputValues) {
    Set<String> rules = ruleValues.stream()
                                  .map(String::toLowerCase)
                                  .collect(toSet());

    return inputValues.stream()
                      .map(String::toLowerCase)
                      .anyMatch(rules::contains);
}
ETO
  • 6,452
  • 1
  • 17
  • 36
1

Equivalent Java 8 code:

    public boolean checkCondition(final List<String> ruleValues, final List<String> inputValues) {

        final Predicate<String> checkRuleValue = ruleValue -> inputValues
            .stream()
            .anyMatch(ruleValue::equalsIgnoreCase);

        return ruleValues
            .stream()
            .anyMatch(checkRuleValue);
    }
Remo
  • 403
  • 3
  • 18