#36 rxjava: 데이터 변환 - map, flatmap
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package org.example.ex10;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import org.example.utils.LogType;
|
||||||
|
import org.example.utils.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FlapMap을 이용한 1 대 다 mapping 예제
|
||||||
|
*/
|
||||||
|
public class ObservableFlatMapEx01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Observable.just("Hello")
|
||||||
|
.flatMap(hello -> Observable.just("자바", "파이썬", "안드로이드").map(lang -> hello + ", " + lang))
|
||||||
|
.subscribe(data -> Logger.log(LogType.ON_NEXT, data));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.example.ex10;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import org.example.utils.LogType;
|
||||||
|
import org.example.utils.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* flapMap을 이용한 구구단의 2단 출력 예제
|
||||||
|
*/
|
||||||
|
public class ObservableFlatMapEx02 {
|
||||||
|
public static void main(String[] args){
|
||||||
|
Observable.range(2, 1)
|
||||||
|
.flatMap(
|
||||||
|
num -> Observable.range(1, 9)
|
||||||
|
.map(row -> num + " * " + row + " = " + num * row)
|
||||||
|
)
|
||||||
|
.subscribe(data -> Logger.log(LogType.ON_NEXT, data));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package org.example.ex10;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import org.example.utils.LogType;
|
||||||
|
import org.example.utils.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FlatMap 두번째 유형을 이용한 구구단의 2단 출력 예제
|
||||||
|
*/
|
||||||
|
public class ObservableFlatMapEx03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Observable.range(2, 1)
|
||||||
|
.flatMap(
|
||||||
|
data -> Observable.range(1, 9),
|
||||||
|
(sourceData, transformedData) ->
|
||||||
|
sourceData + " * " + transformedData + " = " + sourceData * transformedData
|
||||||
|
)
|
||||||
|
.subscribe(data -> Logger.log(LogType.ON_NEXT, data));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.example.ex10;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import org.example.utils.LogType;
|
||||||
|
import org.example.utils.Logger;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observable이 통지한 항목에 함수를 적용하여 통지된 값을 변환시킨다.
|
||||||
|
*/
|
||||||
|
public class ObservableMapEx01 {
|
||||||
|
public static void main(String[] args){
|
||||||
|
List<Integer> oddList = Arrays.asList(1, 3, 5, 7);
|
||||||
|
Observable.fromIterable(oddList)
|
||||||
|
.map(num -> "1을 더한 결과: " + (num + 1))
|
||||||
|
.subscribe(data -> Logger.log(LogType.ON_NEXT, data));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.example.ex10;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import org.example.utils.LogType;
|
||||||
|
import org.example.utils.Logger;
|
||||||
|
|
||||||
|
public class ObservableMapEx02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Observable.just("korea", "america", "canada", "paris", "japan", "china")
|
||||||
|
.filter(country -> country.length() == 5 )
|
||||||
|
.map(country -> country.toUpperCase().charAt(0) + country.substring(1))
|
||||||
|
.subscribe(data -> Logger.log(LogType.ON_NEXT, data));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user