From: Gustavo Martin Morcuende Date: Thu, 8 Dec 2016 22:19:15 +0000 (+0100) Subject: RxJava, using method reference instead of lambdas X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=52f79c0b8f69c90634cac5a88bf6c57677866bba;p=JavaForFun RxJava, using method reference instead of lambdas --- diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java index 0a0f11e..252a7d5 100644 --- a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java +++ b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java @@ -12,18 +12,28 @@ public class AsyncHTTPClient { public void getPages() { getDataAsync("https://github.com/gumartinm") - // fancy Java way of using lambdas. - .subscribe(System.out::println); //The subscribe method starts to run the code implemented in getDataSync - //subscribeOn just declares who is going to run my code (a pool of threads) - //subscribe is the guy who starts to run my code!!! - //JavaScript does the same with Promises but in a cleaner way (IMHO), it does not - //need a subscribe method for starting the machinery (the machinery is underneath - //implemented by the Web Browser with its asynchronous callbacks) - getDataAsync("http://www.google.de").subscribe(page -> { - System.out.println("Another way, no so cool (with lambdas)"); - System.out.println(Thread.currentThread().getName()); - System.out.println(page); - }); + // fancy Java way of using lambdas. Called method reference :) + .subscribe(System.out::println, //The subscribe method starts to run the code implemented in getDataSync + Throwable::printStackTrace);//subscribeOn just declares who is going to run my code (a pool of threads) + //subscribe is the guy who starts to run my code!!! + //JavaScript does the same with Promises but in a cleaner way (IMHO), it does not + //need a subscribe method for starting the machinery (the machinery is underneath + //implemented by the Web Browser with its asynchronous callbacks) + + + getDataAsync("http://www.google.de"). + subscribe(page -> { // It will be called on success :) + System.out.println("Another way, no so cool (with lambdas)"); + System.out.println(Thread.currentThread().getName()); + System.out.println(page); + + }, exception -> exception.printStackTrace()); // It will be called on error. :) + + + // The same with method reference :) + getDataAsync("http://www.google.es"). + subscribe(System.out::println, // It will be called on success :) + Throwable::printStackTrace); // It will be called on error. :) System.out.println("AsyncHTTPClient: YOU SEE ME FIRST!!!!"); diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java index a06bf33..1c6b27d 100644 --- a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java +++ b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java @@ -18,7 +18,14 @@ public class FlatMapTestImpl { .subscribe(word -> { //Unlike JavaScript Promises, we can call many times the same promise without resolving it. //This stuff would be impossible in JavaScript :) System.out.println(word); - }); + }, exception -> exception.printStackTrace()); + + + // The same with method references!!! + getWordsAsync() + .flatMap(Observable::from) + .subscribe(System.out::println, Throwable::printStackTrace); + System.out.println("FlatMapTestImpl: YOU SEE ME FIRST!!!!");