|
|
Java 8 Stream reduce method example.
The Stream.reduce() method is a reduction operation. A reduction operation takes a sequence of input elements and
combines them into a single summary result by repeated application of a combining operation. The Stream.reduce() method comes with
three variations.
Stream.reduce() with Accumulator
It performs a reduction on the elements of the given stream, using given accumulation function.
package com.java2novice.streams;
import java.util.stream.Stream;
public class StreamReduceAccEx {
public static void main(String[] args) {
Stream.of(10,20,22,12,14).reduce((x,y)->x+y).ifPresent(System.out::println);
Stream.of(10,20,22,12,14).reduce(Integer::sum).ifPresent(System.out::println);
Stream.of("java", "c", "c#", "python").reduce((x,y)->x+" | "+y).ifPresent(System.out::println);
}
}
|
|
Output: |
78
78
java | c | c# | python
|
|
Stream.reduce() with Identity and Accumulator
It performs a reduction on the elements of the given stream, using given identity value and an accumulation
function. Here the identity is a starting value.
package com.java2novice.streams;
import java.util.stream.Stream;
public class StreamReduceAccEx {
public static void main(String[] args) {
Integer arrSum = Stream.of(10,20,22,12,14).reduce(1000, Integer::sum);
System.out.println(arrSum);
arrSum = Stream.of(10,20,22,12,14).reduce(1000, (x,y)->x+y);
System.out.println(arrSum);
String result = Stream.of("java", "c", "c#", "python").reduce("Languages:", (x,y)->x+" | "+y);
System.out.println(result);
}
}
|
|
Output: |
1078
1078
Languages: | java | c | c# | python
|
|
Stream.reduce() with Identity, Accumulator and Combiner
It performs a reduction on the elements of the given stream, using given identity value, an accumulation
function and combining functions. The identity value must be an identity for the combiner function. Combiner is a
function which aggregates results of the accumulator. Combiner is called only in a parallel mode to reduce results of
accumulators from different threads.
package com.java2novice.streams;
import java.util.stream.Stream;
public class StreamReduceAccEx {
public static void main(String[] args) {
Integer arrSum = Stream.of(10,20,22,12,14).parallel().reduce(1000, (x,y)->x+y, (p,q)->{
System.out.println("combiner called");
return p+q;
});
System.out.println(arrSum);
}
}
|
|
Output: |
combiner called
combiner called
combiner called
combiner called
5078
|
|
|
|
|
Java 8 Streams Examples
- How Java 8 Streams work?
- Java 8 Streams parallelism introduction.
- Explain non-interference behavior of Java 8 Streams.
- Create Java 8 Stream using Stream.of() method example.
- Create Java 8 Stream using List example.
- Create Java 8 Stream using Stream.generate() method.
- Java 8 Stream.filter() example.
- Java 8 Stream.map() example.
- Java 8 Stream flatmap method example.
- Java 8 Stream peek method example.
- Java 8 Stream distinct method example.
- Java 8 Stream sorted method example.
- Java 8 Stream limit method example.
- Java 8 Stream forEach method example.
- Java 8 Stream toArray method example.
- Java 8 Stream reduce method example.
- Java 8 Stream collect method example.
- Java 8 Stream concat method example.
- Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
- Java 8 Stream findFirst(), findAny() example.
- Primitive type Stream example.
|
|
Can you list serialization methods?
Serialization interface does not have any methods. It is a marker interface.
It just tells the JVM that your class can be serializable.
Before you go and criticize the younger generation, just remember who raised them.
-- Unknown Author
|