I was playing around with Java 8 and I was trying to convert this below code which earlier had for and while loops but unfortunately I could not convert the while to IntStream.
Can someone help me with this. Also if someone can suggest more better and efficient way. Thanks !
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";
}
}
}
The only problem with the code is after changing while to IntStream I am not able to check the condition count < someOtherValues.size() due to which line 65 is resulting in error as count is getting increased more than the ArrayList size.
NOTE : The above code results into java.lang.IndexOutOfBoundsException: Index: 20, Size: 19 You can uncomment the while loop at line 62 and comment the IntStream at line 63 for a fully working code.