#36 rxjava: functional interface ex
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<Car> 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<Car> 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<Car> 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());
|
||||
}
|
||||
}
|
||||
@@ -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<Car> 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<Car> carsFilteredByPrice =
|
||||
CarFilter.filterCarByCustomPredicate(cars, (Car car) -> car.getCarPrice() > 30000000);
|
||||
for(Car car : carsFilteredByPrice)
|
||||
System.out.println("차 이름: " + car.getCarName() + ", 가격: " + car.getCarPrice());
|
||||
|
||||
List<Car> carsFilteredByCarType =
|
||||
CarFilter.filterCarByCustomPredicate(cars, car -> car.getCarType().equals(CarType.SUV));
|
||||
for(Car car : carsFilteredByCarType)
|
||||
System.out.println("차 이름: " + car.getCarName() + ", 차종: " + car.getCarType());
|
||||
}
|
||||
}
|
||||
@@ -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<Car> 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<Car>() {
|
||||
@Override
|
||||
public int compare(Car car1, Car car2) {
|
||||
return car1.getCarPrice() - car2.getCarPrice();
|
||||
}
|
||||
});
|
||||
|
||||
for(Car car : cars)
|
||||
System.out.println("차 이름: " + car.getCarName() + ", 가격: " + car.getCarPrice());
|
||||
}
|
||||
}
|
||||
@@ -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<Car> 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user