Skip to content

Instantly share code, notes, and snippets.

@marilynwaldman
Created January 15, 2018 17:30
Show Gist options
  • Save marilynwaldman/8c0218c2bea6656c752191bbd056f5de to your computer and use it in GitHub Desktop.
Save marilynwaldman/8c0218c2bea6656c752191bbd056f5de to your computer and use it in GitHub Desktop.
Java 8 Map Reduce with Streams - a small example
package com.mdw;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Compute f(g(x)) where
* g(x) = x*x
* and f(x) = 3x + 4
*/
public class App
{
public static void main( String[] args )
{
List<Integer> domain = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> range=
domain.stream()
.map(x -> {
return x * x;
})
.map(x -> {
return 3*x + 4;
})
.collect(Collectors.toList());
//range.forEach(System.out::println);
int result =
range.stream()
.reduce(0, (a, b) -> a + b);
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment