티스토리 뷰
flatMap() 함수
:map 함수는 1대1 함수라면 flatMap함수는 일대다 혹은 일대일 Observable 함수이다.
public class FlatMap {
public static void main(String[] args) {
String[] ball = {"1", "2", "3", "4", "5"};
Observable<String> source = Observable.fromArray(ball)
.flatMap(getDoubleDiamond);
source.subscribe(System.out::println);
}
static Function<String, Observable<String>> getDoubleDiamond = ball -> Observable.just(ball + "<>", ball + "<>");
}
public class Ex4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("GuGuDan Input :" );
int dan = Integer.parseInt(in.nextLine());
for (int row = 1; row <= 9; row++) {
System.out.println( dan + " * " + row + " = " + dan * row);
}
System.out.println("===========================");
// change for to Observable
Observable<Integer> source = Observable.range(1, 9);
source.subscribe(row -> System.out.println( dan + " * " + row + " = " + dan * row));
System.out.println("===========================");
Function<Integer, Observable<String>> gugudan = num ->
Observable.range(1, 9).map(row -> num + " * " + row + " = " + dan * row);
Observable<String> source2 = Observable.just(dan).flatMap(gugudan);
source2.subscribe(System.out::println);
System.out.println("===========================");
Observable<String> source3 = Observable.just(dan)
.flatMap( num -> Observable.range(1, 9)
.map(row -> num + " * " + row + " = " + dan * row)
);
source3.subscribe(System.out::println);
}
}
'RxJava ' 카테고리의 다른 글
fillter() 함수 (0) | 2019.02.13 |
---|---|
map 함수 (0) | 2019.02.07 |
RxJava Subjsct class (0) | 2019.01.18 |
Java8 Function, Consumer, Predicate, Supplier (0) | 2019.01.10 |