Saturday, 15 August 2015

Java8: Streams: Reduce operation

Stream provides reduce method, which is used to perform a reduction on the elements of the stream.

You better understand with simple example, first i am going to show an example, which calculates
         1. Sum of elements in an array
         2. Multiplication of elements in array
         3. Find maximum element in array
         4. Find minimum element in array

using without stream, and with streams.
import java.util.Arrays;
import java.util.List;

public class ReductionWithoutStreams {
 public static void main(String args[]) {
  List<Integer> numbers = Arrays.asList(2, 5, 18, 3, 14, 28, 91, 43, 26);

  /* Perform sum of numbers */
  int sum = 0;
  for (int i : numbers) {
   sum = sum + i;
  }

  /* Perform multiplication of numbers */
  int mul = 1;
  for (int k : numbers) {
   mul = mul * k;
  }

  /* Find maximum number */
  int max = Integer.MIN_VALUE;
  for (int l : numbers) {
   if (l > max) {
    max = l;
   }
  }

  /* Find minimum number */
  int min = Integer.MAX_VALUE;
  for (int m : numbers) {
   if (m < min) {
    min = m;
   }
  }

  System.out.println("Sum of numbers : " + sum);
  System.out.println("Multiplication of numbers : " + mul);
  System.out.println("Maximum of numbers : " + max);
  System.out.println("Minimum of numbers : " + min);
 }
}


Output
Sum of numbers : 230
Multiplication of numbers : 61063360
Maximum of numbers : 91
Minimum of numbers : 2

import java.util.Arrays;
import java.util.List;

public class ReductionWithStreams {
 public static void main(String args[]) {
  List<Integer> numbers = Arrays.asList(2, 5, 18, 3, 14, 28, 91, 43, 26);

  /* Perform sum of numbers */
  int sum = numbers.stream().reduce(0, (a, b) -> a + b);

  /* Perform multiplication of numbers */
  int mul = numbers.stream().reduce(1, (a, b) -> a * b);

  /* Find maximum number */
  int max = numbers.stream().reduce(Integer.MIN_VALUE,
    (a, b) -> ((a > b) ? a : b));

  /* Find minimum number */
  int min = numbers.stream().reduce(Integer.MAX_VALUE,
    (a, b) -> ((a > b) ? b : a));

  System.out.println("Sum of numbers : " + sum);
  System.out.println("Multiplication of numbers : " + mul);
  System.out.println("Maximum of numbers : " + max);
  System.out.println("Minimum of numbers : " + min);
 }
}


Output
Sum of numbers : 230
Multiplication of numbers : 61063360
Maximum of numbers : 91
Minimum of numbers : 2

Lets compare for one operation.

/* Perform sum of numbers */
int sum = 0;
for (int i : numbers) {
         sum = sum + i;
}

Above operation written using stream like below.

int sum = numbers.stream().reduce(0, (a, b) -> a + b);

First parameter of reduce operation is starting value like sum=0.

Next lambda is applied to stream one by one.

First (a,b) -> a + b =  0 + 2 = 2, where 0 is initial value and 2 is first element in the stream.

In 2nd step a is taken as 2 and b is taken as next element in the stream.

(a,b) -> a + b =  2 + 5 = 7.

Same procedure repeats until stream finished.

(a,b) -> a + b =  7 + 18 = 25.
(a,b) -> a + b =  25 + 3 = 28.
(a,b) -> a + b =  28 + 14 = 42.
(a,b) -> a + b =  42 + 28 = 70.              
(a,b) -> a + b =  70 + 91 = 161.
(a,b) -> a + b =  161 + 43 = 204.
(a,b) -> a + b =  204 + 26 = 230


You can write same program using method references like following.
import java.util.Arrays;
import java.util.List;

public class ReductionWithStreams {

 static int mul(int a, int b) {
  return a * b;
 }

 public static void main(String args[]) {
  List<Integer> numbers = Arrays.asList(2, 5, 18, 3, 14, 28, 91, 43, 26);

  /* Perform sum of numbers */
  int sum = numbers.stream().reduce(0, Integer::sum);

  /* Perform multiplication of numbers */
  int mul = numbers.stream().reduce(1, ReductionWithStreams::mul);

  /* Find maximum number */
  int max = numbers.stream().reduce(Integer.MIN_VALUE, Integer::max);

  /* Find minimum number */
  int min = numbers.stream().reduce(Integer.MAX_VALUE, Integer::min);

  System.out.println("Sum of numbers : " + sum);
  System.out.println("Multiplication of numbers : " + mul);
  System.out.println("Maximum of numbers : " + max);
  System.out.println("Minimum of numbers : " + min);
 }
}


Output
Sum of numbers : 230
Multiplication of numbers : 61063360
Maximum of numbers : 91
Minimum of numbers : 2



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment