There is an array:

Integer[] sums = new Integer[5]; 

pre-filled with items.

To output elements c of a new line using functional, use:

 Arrays.stream(sums).forEach(System.out::println); 

How to make a conclusion, for example, in one line, separated by a comma (after the last element, nothing) using java8?

In one line print () , it seems impossible to add a comma inside forEach () . So you still need to check the condition on the last element.

    3 answers 3

    Well, streams are designed to convert each individual element, regardless of the previous ones. Therefore, with the verification of the latter, nothing will come out, if not to kostilizirovat through the limit and skip with the creation of the second copy of the stream.

    If you need exactly the stream, then I will offer only this option:

     System.out.println(Arrays.stream(sums) .map(String::valueOf) .collect(Collectors.joining(", ")) ); 
    • But is there a way to turn the same feint with an array of type int []? The catch is to bring int to String with one method - Donatello
    • one
      mapToObject () solves the issue. thank! - Donatello

    You can, for example, like this:

     System.out.println(Arrays.stream(sums).map(String::valueOf).collect(Collectors.joining(","))); 

      Of course, the crutch method, but I can offer:

       String tmp =""+sums[0]; for(int i=1; i<5; i++){ tmp+=", "+sums[i]; } System.out.print(tmp); 
      • The question was precisely in the use of streams, and not in the use of the usual for - Andrew Bystrov