1. Take - Select first n value
**********************************************
---------------------------------------
import io.reactivex.Observable;
public class TestClass {
public static void main(String[] args){
Observable.just("One", "Two", "Three")
.take(2)
.subscribe(value -> {
System.out.println(value);
});
}
}
************************************************
2. Skip - Select after first n value
--------------------------------------
import io.reactivex.Observable;
public class TestClass {
public static void main(String[] args){
Observable.just("One", "Two", "Three")
.skip(2)
.subscribe(value -> {
System.out.println(value);
});
}
}
3. Map - Transform one type to another
Example below - Convert number to string
--------------------------------------
import io.reactivex.Observable;
public class TestClass {
public static void main(String[] args){
Observable.just(1, 2, 3)
.map(number -> String.valueOf(number))
.subscribe(value -> {
System.out.println(value);
});
}
}
4. What is RxJava
---------------------------------------------------
A library for composing Asynchronous and Event-based program using observable sequences for
Java VM.
---------------------------------------------------
5. filter
**********************************************
-----------------------------------------------------
Observable
.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.filter(integer -> integer % 2 == 0)
.subscribe(System.out::println);
-----------------------------------------------------
**********************************************
6. range
**********************************************
-----------------------------------------------------
Observable
.range(1, 5)
.subscribe(System.out::println);
-----------------------------------------------------
**********************************************
7. zipwith
**********************************************
-----------------------------------------------------
List<String> list = new ArrayList<>();
list.add("List-1");
list.add("List-2");
list.add("List-3");
list.add("List-4");
list.add("List-5");
list.add("List-6");
list.add("List-7");
list.add("List-8");
list.add("List-9");
list.add("List-10");
list.add("List-11");
list.add("List-12");
Observable.fromIterable(list)
.zipWith(Observable.range(1, Integer.MAX_VALUE),
(string, count)->String.format("%2d. %s", count, string))
.subscribe(System.out::println);
Exp-2
----------------------------------------------------
List<String> list = new ArrayList<>();
list.add("List-1");
list.add("List-2");
list.add("List-3");
list.add("List-4");
list.add("List-5");
list.add("List-6");
list.add("List-7");
list.add("List-8");
list.add("List-9");
list.add("List-10");
list.add("List-11");
list.add("List-12");
List<Integer> list2 = new ArrayList<>();
list2.add(1);
list2.add(2);
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(12);
Observable.fromIterable(list)
.zipWith(list2,
(string, count)->String.format("%2d. %s", count, string))
.subscribe(System.out::println);
----------------------------------------------------
**********************************************
8. distinct
**********************************************
-----------------------------------------------------
List<Integer> list2 = new ArrayList<>();
list2.add(1);
list2.add(2);
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list2.add(8);
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(11);
list2.add(12);
Observable.fromIterable(list2)
.distinct()
.subscribe(System.out::println);
-----------------------------------------------------
**********************************************
9. sorted
**********************************************
-----------------------------------------------------
List<Integer> list2 = new ArrayList<>();
list2.add(1);
list2.add(2);
list2.add(7);
list2.add(8);
list2.add(8);
list2.add(9);
list2.add(10);
list2.add(3);
list2.add(11);
list2.add(4);
list2.add(5);
list2.add(5);
list2.add(6);
list2.add(11);
list2.add(12);
Observable.fromIterable(list2)
.distinct()
.sorted()
.subscribe(System.out::println);
-----------------------------------------------------
**********************************************
Iterable
pull
T next()
throws
22
No comments:
Post a Comment