This may get a little bit complex if you actually want the result as:
1+2+3 =6
2+3+4 = 9
3+4 = 7
4=4
In which case you can either do it as follows:
String result = IntStream.range(0, array.length)
.mapToObj(i -> Arrays.stream(array).skip(i).limit(Math.min(3, array.length-i)).toArray())
.map(a -> String.join("+",
Arrays.stream(a).mapToObj(String::valueOf).toArray(String[]::new)) + "=" +
Arrays.stream(a).sum())
.collect(Collectors.joining("\n"));
System.out.println(result);
or as follows:
IntStream.range(0, array.length)
.mapToObj(i -> Arrays.stream(array).skip(i).limit(Math.min(3, array.length-i)).toArray())
.map(a -> String.join("+",
Arrays.stream(a).mapToObj(String::valueOf).toArray(String[]::new)) + "=" +
Arrays.stream(a).sum())
.forEachOrdered(System.out::println);
However, if the expected result can be as simple as printing the result of each group summation then it can be done as follows:
IntStream.range(0, array.length)
.map(i -> Arrays.stream(array).skip(i).limit(Math.min(3, array.length-i)).sum())
.forEachOrdered(System.out::println);
Explanation:
The first code snippet uses Instream.range to generate the indices of the source array, then within the mapToObj operation it skips the necessary amount of elements and retains only the necessary amount of elements. Then with map we join the elements in each group as well as calculating the summation of them and then as for getting it in the format we want, finally, we join the strings with the joining collector.
The second code snippet does the same but instead of joining the strings with the joining collector, simply prints them to the console.
The last approach does a similar thing in regards to the mapping but is only concerned with the result of each group summation.
123, shouldn't it be1234?IntStream.range(0, list.size()) .map(i -> list.stream().skip(i).limit(Math.min(3, list.size()-i)).mapToInt(Integer::intValue).sum()) .forEachOrdered(System.out::println);.