Last active
December 13, 2015 19:48
-
-
Save benjchristensen/4965520 to your computer and use it in GitHub Desktop.
Example of RxJava being used with Java 8 lambdas
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
import rx.Observable; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class RxUsingJava8 { | |
public static void main(String args[]) { | |
/* | |
* Example using single-value lambdas (Func1) | |
*/ | |
Observable.from(1, 2, 3, 4, 5) | |
.filter((v) -> { | |
return v < 4; | |
}) | |
.subscribe((value) -> { | |
System.out.println("Value: " + value); | |
}); | |
/* | |
* Example with 'reduce' that takes a lambda with 2 arguments (Func2) | |
*/ | |
Observable.from(1, 2, 3, 4, 5) | |
.reduce((seed, value) -> { | |
// sum all values from the sequence | |
return seed + value; | |
}) | |
.map((v) -> { | |
return "DecoratedValue: " + v; | |
}) | |
.subscribe((value) -> { | |
System.out.println(value); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More information on RxJava can be found at https://github.com/Netflix/RxJava