diff --git a/RxJava/practice/src/main/java/org/example/common/SampleData.java b/RxJava/practice/src/main/java/org/example/common/SampleData.java index 77f7a878..4c82aa19 100644 --- a/RxJava/practice/src/main/java/org/example/common/SampleData.java +++ b/RxJava/practice/src/main/java/org/example/common/SampleData.java @@ -1,5 +1,7 @@ package org.example.common; +import io.reactivex.rxjava3.core.Observable; + import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; diff --git a/RxJava/practice/src/main/java/org/example/ex06/FunctionalInterfaceEx.java b/RxJava/practice/src/main/java/org/example/ex06/FunctionalInterfaceEx.java new file mode 100644 index 00000000..75fb0f26 --- /dev/null +++ b/RxJava/practice/src/main/java/org/example/ex06/FunctionalInterfaceEx.java @@ -0,0 +1,40 @@ +package org.example.ex06; + +import org.example.common.*; + +import java.util.Arrays; +import java.util.List; + +/** + * 사용자 정의 Predicate를 익명 클래스로 구현하는 예제 + */ +public class FunctionalInterfaceEx { + public static void main(String[] args) { + List cars = Arrays.asList( + new Car(CarMaker.HYUNDAE, CarType.SUV, "팰리세이드", 28000000, true), + new Car(CarMaker.SAMSUNG, CarType.SEDAN, "SM5", 35000000, true), + new Car(CarMaker.CHEVROLET, CarType.SUV, "트래버스", 50000000, true), + new Car(CarMaker.KIA, CarType.SEDAN, "K5", 20000000, false), + new Car(CarMaker.SSANGYOUNG, CarType.SUV, "티볼리", 23000000, true) + ); + + List carsFilteredByPrice = CarFilter.filterCarByCustomPredicate(cars, new CarPredicate() { + @Override + public boolean test(Car car) { + return car.getCarPrice() > 30000000; + } + }); + + for(Car car : carsFilteredByPrice) + System.out.println("차 이름: " + car.getCarName() + ", 가격: " + car.getCarPrice()); + + List carsFilteredByCarType = CarFilter.filterCarByCustomPredicate(cars, new CarPredicate() { + @Override + public boolean test(Car car) { + return car.getCarType().equals(CarType.SUV); + } + }); + for(Car car : carsFilteredByCarType) + System.out.println("차 이름: " + car.getCarName() + ", 차종: " + car.getCarType()); + } +} \ No newline at end of file diff --git a/RxJava/practice/src/main/java/org/example/ex06/FunctionalInterfaceToLamdaEx.java b/RxJava/practice/src/main/java/org/example/ex06/FunctionalInterfaceToLamdaEx.java new file mode 100644 index 00000000..11b06864 --- /dev/null +++ b/RxJava/practice/src/main/java/org/example/ex06/FunctionalInterfaceToLamdaEx.java @@ -0,0 +1,34 @@ +package org.example.ex06; + +import org.example.common.Car; +import org.example.common.CarFilter; +import org.example.common.CarMaker; +import org.example.common.CarType; + +import java.util.Arrays; +import java.util.List; + +/** + * 사용자 정의 Predicate를 람다 표현식으로 사용하는 예제 + */ +public class FunctionalInterfaceToLamdaEx { + public static void main(String[] args) { + List cars = Arrays.asList( + new Car(CarMaker.HYUNDAE, CarType.SUV, "팰리세이드", 28000000, true), + new Car(CarMaker.SAMSUNG, CarType.SEDAN, "SM5", 35000000, true), + new Car(CarMaker.CHEVROLET, CarType.SUV, "트래버스", 50000000, true), + new Car(CarMaker.KIA, CarType.SEDAN, "K5", 20000000, false), + new Car(CarMaker.SSANGYOUNG, CarType.SUV, "티볼리", 23000000, true) + ); + + List carsFilteredByPrice = + CarFilter.filterCarByCustomPredicate(cars, (Car car) -> car.getCarPrice() > 30000000); + for(Car car : carsFilteredByPrice) + System.out.println("차 이름: " + car.getCarName() + ", 가격: " + car.getCarPrice()); + + List carsFilteredByCarType = + CarFilter.filterCarByCustomPredicate(cars, car -> car.getCarType().equals(CarType.SUV)); + for(Car car : carsFilteredByCarType) + System.out.println("차 이름: " + car.getCarName() + ", 차종: " + car.getCarType()); + } +} \ No newline at end of file diff --git a/RxJava/practice/src/main/java/org/example/ex06/LegacyInterfaceEx.java b/RxJava/practice/src/main/java/org/example/ex06/LegacyInterfaceEx.java new file mode 100644 index 00000000..c93e42e3 --- /dev/null +++ b/RxJava/practice/src/main/java/org/example/ex06/LegacyInterfaceEx.java @@ -0,0 +1,35 @@ +package org.example.ex06; + +import org.example.common.Car; +import org.example.common.CarMaker; +import org.example.common.CarType; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * 하나의 추상 메서드를 가지고 있는 기존 인터페이스를 구현하는 예제 + */ +public class LegacyInterfaceEx { + public static void main(String[] args) { + List cars = Arrays.asList( + new Car(CarMaker.HYUNDAE, CarType.SUV, "팰리세이드", 28000000, true), + new Car(CarMaker.SAMSUNG, CarType.SEDAN, "SM5", 35000000, true), + new Car(CarMaker.CHEVROLET, CarType.SUV, "트래버스", 50000000, true), + new Car(CarMaker.KIA, CarType.SEDAN, "K5", 20000000, false), + new Car(CarMaker.SSANGYOUNG, CarType.SUV, "티볼리", 23000000, true) + ); + + Collections.sort(cars, new Comparator() { + @Override + public int compare(Car car1, Car car2) { + return car1.getCarPrice() - car2.getCarPrice(); + } + }); + + for(Car car : cars) + System.out.println("차 이름: " + car.getCarName() + ", 가격: " + car.getCarPrice()); + } +} \ No newline at end of file diff --git a/RxJava/practice/src/main/java/org/example/ex06/LegacyInterfaceToFunctionalInterfaceEx.java b/RxJava/practice/src/main/java/org/example/ex06/LegacyInterfaceToFunctionalInterfaceEx.java new file mode 100644 index 00000000..b6a619b4 --- /dev/null +++ b/RxJava/practice/src/main/java/org/example/ex06/LegacyInterfaceToFunctionalInterfaceEx.java @@ -0,0 +1,30 @@ +package org.example.ex06; + +import org.example.common.Car; +import org.example.common.CarMaker; +import org.example.common.CarType; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * 하나의 추상 메서드를 가지고 있는 기존 인터페이스를 함수형 인터페이스로 사용하는 예제 + */ +public class LegacyInterfaceToFunctionalInterfaceEx { + public static void main(String[] args) { + + List cars = Arrays.asList( + new Car(CarMaker.HYUNDAE, CarType.SUV, "팰리세이드", 28000000, true), + new Car(CarMaker.SAMSUNG, CarType.SEDAN, "SM5", 35000000, true), + new Car(CarMaker.CHEVROLET, CarType.SUV, "트래버스", 50000000, true), + new Car(CarMaker.KIA, CarType.SEDAN, "K5", 20000000, false), + new Car(CarMaker.SSANGYOUNG, CarType.SUV, "티볼리", 23000000, true) + ); + + Collections.sort(cars, (car1, car2) -> car1.getCarPrice() - car2.getCarPrice()); + + for(Car car : cars) + System.out.println("차 이름: " + car.getCarName() + ", 가격: " + car.getCarPrice()); + } +} \ No newline at end of file