【发布时间】:2021-08-05 18:17:01
【问题描述】:
我正在使用 Java 8,我试图将下面的代码转换为之前有 for 和 while 循环的代码,但不幸的是我无法将 while 转换为 IntStream。
有人可以帮我解决这个问题吗?另外,如果有人可以提出更好,更有效的方法。谢谢!
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
public class NestedStreams {
public static void main(String[] args) {
ArrayList<String> a1 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "01", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a2 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "02", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a3 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "03", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a4 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "04", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a5 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "05", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a6 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a7 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a8 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a9 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a10 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a11 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<String> a12 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
ArrayList<ArrayList<String>> someList = new ArrayList<ArrayList<String>>();
someList.addAll(Arrays.asList(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12));
NestedStreams ns = new NestedStreams();
String status = ns.testMethod(someList);
if (status.equalsIgnoreCase("working")) {
System.out.println("we can now do the processing");
} else {
System.out.println("failure");
}
}
public String testMethod(ArrayList<ArrayList<String>> someList) {
try {
IntStream.range(0, someList.size()).forEach(i -> {
ArrayList<String> someOtherValues = (ArrayList<String>) someList.get(i);
someOtherValues.replaceAll(t -> Objects.isNull(t) ? "" : t);
AtomicInteger count = new AtomicInteger(4);
AtomicInteger counter = new AtomicInteger(5);
if (!someOtherValues.get(0).toString().equals("")) {
// while (count.intValue() < (someOtherValues.size())) { //Line 62
IntStream.range(count.intValue(), someOtherValues.size()).forEach(value -> { //Line 63
IntStream.range(0, 3).forEach(k -> {
String avalue = someOtherValues.get(count.intValue()).toString();//count is getting increased more than the length of arrayList //Line 65
System.out.println(avalue);
counter.incrementAndGet();
});
count.set(counter.intValue());
counter.incrementAndGet();
System.out.println("The variable value :-" + value);
System.out.println("The variable counter :-" + counter);
System.out.println("The variable count :-" + count);
});
}
});
return "working";
} catch (Exception e) {
e.printStackTrace();
return "failed";
}
}
}
代码的唯一问题是在更改为 IntStream 后,我无法检查条件 count
注意:上面的代码导致 java.lang.IndexOutOfBoundsException: Index: 20, Size: 19 您可以在第 62 行取消注释 while 循环并在第 63 行注释 IntStream 以获得完整的工作代码。
【问题讨论】:
-
在while循环中你使用
someOtherValues.size(),但在第63行你使用someList.size()。为什么? -
这是一个错字@Alex。我真的打算包括我在每次迭代 i 变量后得到的那些列表。感谢您指出这一点
-
修好了之后还能用吗?
-
不,这不是修复,这就是它的意思,我实际上打错了,当我在 Eclipse 上检查我的代码时,它是 someOtherValues.size()。当前编辑的代码会出错。
标签: java lambda java-8 functional-programming java-ee-8