2

I have an array like 1,2,3,4 need to divide into

int [] array = { 1, 2, 3 , 4}

for( int i= 0; i < array .length-0; i ++) {
  Integer[] intArrays = array .stream().toArray(Integer[]::new);
 //processing with stream
}


123, 234,34,4
sum of the each grouping 
1+2+3 =6
2+3+4 = 9
3+4 = 7
4=4

I can do it from java traditional way but need to implement using java 8 stream concept

can you help me to do that

12
  • 2
    The groupings don't make sense. Is the first one supposed to be 1+2+3+4? And why is the last grouping 4+4? Maybe just 4? Commented Jul 14, 2018 at 22:44
  • sorry it should be eqaul sign Commented Jul 14, 2018 at 22:44
  • if you can do it 123, 234 that also enough for me Commented Jul 14, 2018 at 22:48
  • @Kasunshan why is the first 123, shouldn't it be 1234? Commented Jul 14, 2018 at 22:51
  • 1
    @Kasunshan 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); . Commented Jul 15, 2018 at 0:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use Arrays.stream(array, i, Math.min(i+3, array.length)) instead of chaining skip and limit, but when the only chained operation is toArray, you can use Arrays.copyOfRange(array, i, Math.min(i+3, array.length)) in the first place. And I wouldn’t use toArray, just to call String.join when there is collect(Collectors.joining(…)); this collector can also get combined with the second stream operation: .map(a -> Arrays.stream(a).mapToObj(String::valueOf) .collect(Collectors.joining("+", "", "="+Arrays.stream(a).sum())))
2

I'm assuming the request is to limit each grouping to 3 elements. If so,

List<Integer> l = Arrays.asList(1, 2, 3, 4);

for (int i = 0; i < l.size(); i++) {
  l.set(i, l.stream().skip(i).limit(3).mapToInt(Integer::intValue).sum());
}

1 Comment

or you could do IntStream.range(0, l.size()).forEachOrdered(i -> l.set(i, l.stream().skip(i).limit(3).mapToInt(Integer::intValue).sum())); if you wanted to avoid the external iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.