rxjava : lambda

This commit is contained in:
haerong22
2022-03-15 13:02:08 +09:00
parent b807790cd1
commit bed29f3233
5 changed files with 109 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package _01_lambda;
public interface ExInterface {
void getMsg();
default String defaultMethod() {
return "default method";
}
static void print(String msg) {
System.out.println("msg = " + msg);
}
}

View File

@@ -0,0 +1,17 @@
package _01_lambda;
public class LambdaEx_01 {
public static void main(String[] args) {
ExInterface.print("interface static method test");
ExInterface exInterface = () -> System.out.println("implement abstract method");
exInterface.getMsg();
String result = exInterface.defaultMethod();
System.out.println("result = " + result);
}
}

View File

@@ -0,0 +1,18 @@
package _01_lambda;
public class LambdaEx_02 {
public static void main(String[] args) {
// 람다 사용 X
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Run Thread");
}
}).start();
// 람다 사용
new Thread(() -> System.out.println("Lambda Run Thread")).start();
}
}

View File

@@ -0,0 +1,46 @@
package _01_lambda;
public class LambdaEx_03 {
@FunctionalInterface
interface MyFunction1 {
int calc(int a);
}
@FunctionalInterface
interface MyFunction2 {
int calc(int a, int b);
}
@FunctionalInterface
interface MyFunction3 {
void print();
}
public static void main(String[] args) {
// 파라미터가 1개인 람다식
MyFunction1 result1 = (x) -> { return x + x; };
MyFunction1 result2 = x -> x + x; // 괄호 생략 가능
System.out.println("result1 = " + result1.calc(5));
System.out.println("result2 = " + result2.calc(5));
// 파라미터가 2개인 람다식
MyFunction2 add = (x, y) -> x + y;
MyFunction2 minus = (x, y) -> x - y;
System.out.println("add = " + add.calc(1, 2));
System.out.println("minus = " + minus.calc(1, 2));
// 파라미터가 없는 람다식
MyFunction3 myFunction3 = () -> System.out.println("print method");
myFunction3.print();
// 함수형 인터페이스 파라미터로 전달
printSum(3, 4, (x, y) -> x * y);
}
static void printSum(int x, int y, MyFunction2 f) {
System.out.println(f.calc(x, y));
}
}

View File

@@ -0,0 +1,14 @@
package _01_lambda;
public class LambdaEx_04 {
@FunctionalInterface
interface MyFunction3 {
void print();
}
public static void main(String[] args) {
//
}
}