티스토리 뷰

RxJava

flatMap() 함수

구름나드리 2019. 1. 23. 11:46



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
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함