Created
January 15, 2018 17:30
-
-
Save marilynwaldman/8c0218c2bea6656c752191bbd056f5de to your computer and use it in GitHub Desktop.
Java 8 Map Reduce with Streams - a small example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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