【发布时间】:2021-04-13 15:46:04
【问题描述】:
我正在尝试将以下代码转换为 Java 8(使用流),但不确定如何使用流精确地遍历 2 个列表。
public class ComparisonString {
private static List<String> input;
public static void main(String[] args) {
input = Arrays.asList("TRUE","FALSE","TRUE","N/A");
ComparisonString cs = new ComparisonString();
System.out.println(cs.getMatchedOutput(Arrays.asList("TRUE","N/A","N/A","FALSE")));
}
/**
* Check whether the 'input' List and the 'givenInput' List match.
* If a value is "N/A" in either lists then it means does-not-matter/don't-check-for-a-match-and-go-next
* @param givenInput
* @return
*/
public Optional<String> getMatchedOutput(final List<String> givenInput) {
boolean flag = true;
for (int i = 0; i < givenInput.size(); i++) {
if (this.input.get(i) != null
&& !(this.input.get(i).equalsIgnoreCase("N/A")
|| givenInput.get(i).equalsIgnoreCase("N/A"))
&& !this.input.get(i).equalsIgnoreCase(givenInput.get(i))) {
flag = false;
break;
}
}
if (flag) {
return Optional.of("flag = true");
} else {
return Optional.empty();
}
}
}
【问题讨论】:
标签: java for-loop java-8 iterator java-stream