Map Reduce Example in Java 8

The map-reduce concept is one of the powerful concept in computer programming which utilizes the power of distributed and parallel processing to solve a big and heavy problems in quick time. From Java 8 onwards, Java also got this powerful feature from functional programming world. Many of the services provided by internet e.g. Google Search are based on the concept of the map and reduce. In  map reduce a job is usually split into the input data-set into independent chunks which are processed by the map tasks in a completely parallel manner. The framework sorts the outputs of the maps, which are then inputted to the reduce tasks. For example, suppose you want to calculate the average age of all the people in a town, instead of counting sequentially you can first calculate their age using the map and then calculate the average using reduce.

Map Reduce Example in Java 8

In this Java 8 tutorial , we will go over the map function in Java 8. It is used to implement the MapReduce type operations. Essentially we map a set of values then we reduce it with a function such as average or sum into a single number. Let's take a look at this sample which will do an average of numbers the old and new ways.

Again, the old method is many lines of code and is designed to do this all very sequentially. This code could not take advantage of a multi-core processor. Sure, in this example it is very simple, but imagine if you have millions of items you are processing and you have an 8 core machine. Seven of those core would be completely wasted and idle during this long calculation.

Now if we look at the simple one-line Java 8 way of doing it:

double average => p.getAge())

This uses the concept of parallelism, where it creates a parallel stream out of the array, which can be processed by multiple cores and then finally joined back into to map the results together. The map function will create a stream containing only the values with meet the given criteria, then the average function will reduce this map into a single value. Now all 8 of your cores are processing this calculation so it should run much faster.

A picture is worth a thousand word, this is truly justified by following diagram which clearly highlights how map-reduce work in practice.

Map Reduce Example in Java 8

Java Program to demonstrate Map reduce operation
Here is our Java program which will teach you how to use the map and reduce in Java 8. The reduce operation is also known as the fold in the functional programming world and very helpful with Collection class which holds a lot of items. You can perform a lot of bulk operation, calculating stats using the map and reduce in Java 8. As a Java developer you must know how to use map(), flatMap() and filter() method, these three are key methods for doing functional programming in Java 8.

If you are interested you can also read Java 8 in Action by Raoul-Gabriel Urma to learn functional programming in Java 8.

package test;
import java.util.ArrayList;
import java.util.List;
 * Java Program to demonstrate how to do map reduce in Java. Map, reduce also
 * known as fold is common operation while dealing with Collection in Java.

 * @author Javin Paul
public class Test {
    public static void main(String args[]) {
        List<Employee> peoples = new ArrayList<>();
        peoples.add(new Employee(101, "Victor", 23));
        peoples.add(new Employee(102, "Rick", 21));
        peoples.add(new Employee(103, "Sam", 25));
        peoples.add(new Employee(104, "John", 27));
        peoples.add(new Employee(105, "Grover", 23));
        peoples.add(new Employee(106, "Adam", 22));
        peoples.add(new Employee(107, "Samy", 224));
        peoples.add(new Employee(108, "Duke", 29));
        double average = calculateAverage(peoples);
        System.out.println("Average age of employees are (classic way) : " 
                            + average);
        average = average(peoples);
        System.out.println("Average age of employees are (lambda way) : " 
                            + average);
     * Java Method to calculate average from a list of object without using
     * lambdas, doing it on classical java way.
     * @param employees
     * @return average age of given list of Employee
    private static double calculateAverage(List<? extends Employee> employees){
        int totalEmployee = employees.size();
        double sum = 0;
        for(Employee e : employees){
            sum += e.getAge();
        double average = sum/totalEmployee;
        return average;
     * Java method which uses map reduce to calculate average of list of employees
     * in JDK 8.
     * @param peoples
     * @return average age of given list of Employees
    private static double average(List<? extends Employee> peoples){
        return> p.getAge()).average().getAsDouble();
class Employee{
    private final int id;
    private final String name;
    private final int age;
    public Employee(int id, String name, int age){ = id; = name;
        this.age = age;
    public int getId(){
        return id;
    public String getName(){
        return name;
    public int getAge(){
        return age;
Average age of employees are (classic way) : 49.25
Average age of employees are (lambda way) : 49.25

That's all about how to do map-reduce in Java 8.  This is rather a simple example of a powerful concept like map reduce but most important thing is that now you can use this concept in Java 8 with built-in map() and reduce() method of Stream class. If you want to learn more about functional programming in Java 8, I suggest to read Functional Programming in Java: Harnessing the Power Of Java 8 Lambda Expressions By  Venkat Subramaniam, one of the best books to start with functional programming in Java.

How to use Map Reduce in Java 8

Other Java 8 tutorials you may like
  • 10 Example of Lambda Expression in Java 8 (see here)
  • 10 Example of forEach() method in Java 8 (example)
  • 10 Example of Joining String in Java 8 (see here
  • 10 Example of Stream API in Java 8 (see here)
  • How to use peek() method of Stream in Java 8? (example)
  • 10 Example of converting a List to Map in Java 8 (example)
  • 20 Example of LocalDate and LocalTime in Java 8 (see here)
  • Difference between map() and flatMap() in Java 8? (answer)
  • How to use Stream.flatMap in Java 8(example)
  • How to use in Java 8 (example)
  • 5 Books to Learn Java 8 and Functional Programming (list)

Official Java 8 tutorial
Java SE 8 Lambda quick start


  1. If you are performing map-reduce in large scale, which is always the case, you should use parallelStream() instead of stream() to get full advantage of multiple cores of your server.

  2. Yes I think Javin is right. If you are using stream it will still be executed on a single core just like a normal sequential routine. For using parallelism we will have to use parallelSteam.