[英]How to create something equivalent to the following logic using java8 streams?
I wish to create a method equivalent to the following using Java 8 streams but not able to do this.我希望使用 Java 8 流创建一个等效于以下方法但无法执行此操作。 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;
}
Try this approach.试试这个方法。 It will run in
O(n)
time:它将在
O(n)
时间内运行:
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);
}
Equivalent Java 8 code:相当于Java 8码:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.