Skip to content

Instantly share code, notes, and snippets.

@benjchristensen
Last active December 13, 2015 19:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benjchristensen/4965520 to your computer and use it in GitHub Desktop.
Save benjchristensen/4965520 to your computer and use it in GitHub Desktop.
Example of RxJava being used with Java 8 lambdas
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);
});
}
}
@benjchristensen
Copy link
Author

Output of the above code is:

Value: 1
Value: 2
Value: 3
DecoratedValue: 15

@benjchristensen
Copy link
Author

More information on RxJava can be found at https://github.com/Netflix/RxJava

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment