text: In this section, 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.
  import java.util.*;
  import java.util.OptionalDouble;
  import java.util.stream.*;
  public class UserAverageTest {() {
  int total = 0;
  for (User u : users) {
  total += u.age;
  }
  double average = (double)total / (double)users.size();
  System.out.println("OLDWAY Average User Age: " + average);
  }
  private static void newJavaWay() {
  double average = users.parallelStream().map(u -> u.age).average().getAsDouble();
  System.out.println("NEWWAY Average User Age: " + average);
  }
  }
  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 cores 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 = users.parallelStream().map(u -> u.age).average().getAsDouble();
  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.
  Prev (Functions) |
  Next (Listeners)