Close

Java 8 Streams - Stream.reduce Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.Stream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.StreamStreamLogicBig

Methods:

These terminal operation performs a reduction on the elements.

T reduce(T identity,
         BinaryOperator<T> accumulator)
Parameters:
identity - the identity value for the accumulating function
accumulator - a function for combining two values



Optional<T> reduce(BinaryOperator<T> accumulator)
Parameters:
accumulator - a function for combining two values.



<U> U reduce(U identity,
             BiFunction<U,? super T,U> accumulator,
             BinaryOperator<U> combiner)
Parameters:
identity - the identity value for the combiner function
accumulator - a function for incorporating an additional element into a result
combiner - a function for combining two values, which must be compatible with the accumulator function


Examples


package com.logicbig.example.stream;

import java.util.Arrays;
import java.util.Optional;

public class ReduceExample {

public static void main(String... args) {
String[] array = {"one", "two", "three", "four"};
Optional<String> opt = Arrays.stream(array).reduce((s, s2) -> s + "-" + s2);

if (opt.isPresent()) {
System.out.println(opt.get());
}
}
}

Output

one-two-three-four




package com.logicbig.example.stream;

import java.util.Arrays;

public class ReduceExample2 {

public static void main(String... args) {
String[] array = {"one", "two", "three", "four"};
String result = Arrays.stream(array)
.reduce("", (s, s2) -> s + "-" + s2);

System.out.println(result);
}
}

Output

-one-two-three-four




package com.logicbig.example.stream;

import java.util.Arrays;

public class ReduceExample3 {

public static void main(String... args) {
String[] array = {"one", "two", "three", "four"};
String result = Arrays.stream(array)
.reduce("",
(s, s2) -> s + "-" + s2,
(s, s2) -> s + "|" + s2);
System.out.println(result);
}
}

Output

-one-two-three-four




Combiner is only used in parallel stream to combine different results from different threads.

package com.logicbig.example.stream;

import java.util.Arrays;

public class ReduceExample4 {

public static void main(String... args) {
String[] array = {"one", "two", "three", "four"};
String result = Arrays.stream(array)
.parallel()
.reduce("",
(s, s2) -> s + "-" + s2,
(s, s2) -> s + "|" + s2);
System.out.println(result);
}
}

Output

-one|-two|-three|-four

This examples shows how to use IntStream#reduce(IntBinaryOperator op) method.

package com.logicbig.example;

import java.util.stream.IntStream;

public class ReduceExample1 {
public static void main (String[] args) {
int i = IntStream.range(1, 6)
.reduce((a, b) -> a * b)
.orElse(-1);

System.out.println(i);
}
}

Output

120
Original Post




Using IntStream#reduce(int identity, IntBinaryOperator op)

package com.logicbig.example;

import java.util.stream.IntStream;

public class ReduceExample2 {
public static void main (String[] args) {
int i = IntStream.range(1, 6)
.parallel()
.reduce(1, (a, b) -> a * b);

System.out.println(i);
}
}

Output

120
Original Post




Using Stream#reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) method

package com.logicbig.example;

import java.util.stream.Stream;

public class ReduceExample3 {
public static void main (String[] args) {
int i = Stream.of("2", "3", "4", "5")
.parallel()
.reduce(0, (integer, s) -> Integer.sum(integer, Integer.parseInt(s)),
(integer, integer2) -> Integer.sum(integer, integer2));

System.out.println(i);
}
}

Output

14
Original Post




See Also