JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
Tomorrow is often the busiest day of the week.
-- Spanish Proverb

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.