Java/Java

Java 13. 람다식

shin96bc 2022. 3. 11. 11:59

(1) 익명 함수(Anonymous functions)를 지칭하는 용어이다.

 

(2) 파라미터와 리턴값을 달리한 람다식 예
     ex) interface AI1{
               void m1();
          }
          interface AI2{
               void m2(int i);
          }
          interface AI3{
               int m3(int i, int j);
          }

          class A {
               void use1(){
                    //AI1 ai1 = () -> {System.out.println("m1()구현1");};
                    AI1 ai1 = () -> System.out.println("m1()구현1"); ( 한줄일 경우 {} 를 생략할 수 있다. )
                    ai1.m1();
               }
               void use2(){
                    AI2 ai2 = (i) -> {
                         int r = i+1;
                         System.out.println("m2()구현2 r: " + r);
                    };
                    ai2.m2(1);
                    }

               void use3(){
                    AI3 ai3 = (i, j) -> i+j;
                    int sum = ai3.m3(10, 20);
                    System.out.println("sum: " + sum);
               }
               void use33(){
                    AI3 ai3 = (i, j) -> multiply(i,j);
                    int mul = ai3.m3(10, 20);
                    System.out.println("mul: " + mul);
               }
               int multiply(int i, int j){
               return i*j;
          }

 

(3) 내부클래스의 람다식에서 멤버변수를 접근하는 예
     ex) interface BI{
               void m();
         }
         class BOut {
               int i = 1;
               class BIn {
                    int j = 2;
                    void use(){
                         //i = 11; // 멤버 변수는 변경 가능!
                         //j = 22; // 멤버 변수는 변경 가능!
                         BI bi = () -> {
                             //System.out.println("i: " + BOut.this.i); // BOut.this 는 생략이 가능하다.
                             System.out.println("i: " + i);
                             //System.out.println("j: " + this.j); // this 는 생략이 가능하다.
                             System.out.println("j: " + j);
                         };
                         bi.m();
                    }
               }
         }
         class B {
              public static void main(String[] args) {
                   BOut.BIn bin = new BOut().new BIn();
                   bin.use();
              }
         }

 

(4) 람다식에서 지역변수를 접근하는 예
      ex) interface CI{
               void m();
           }

           class C {
                void use(int i){
                      int j = 2;
                      //i = 11; // 지역변수는 변경 불가!! final 특성이 있다.
                      //j = 22; // 지역변수는 변경 불가!! final 특성이 있다.

                      CI ci = () -> {
                           System.out.println("i: " + i);
                           System.out.println("j: " + j);
                      };
                      ci.m();
                      }
                      public static void main(String[] args) {
                           C c = new C();
                           c.use(1);
                      }
           }
 

(5) 쓰레드의 Runnable 을 구현하는 람다식 예
      ex) class RunChild implements Runnable {
                public void run(){
                     while(true){
                          System.out.println("쓰레드의 일1");
                          try{
                               Thread.sleep(1000);
                          }catch(Exception e){}
                     }
                }
           }

           class D {
                void use(){
                     //방식1
                     //Runnable r = new RunChild();
                     //Thread th = new Thread(r);

                     //방식2
                     /*Runnable r = new RunChild(){
                          public void run(){
                              while(true){
                                   System.out.println("쓰레드의 일2");
                                   try{
                                        Thread.sleep(1000);
                                  }catch(Exception e){}
                              }
                          }
                       };
                       Thread th = new Thread(r);
                       */

                       //방식3
                       /*Runnable r = () -> {
                            while(true){
                                 System.out.println("쓰레드의 일3");
                                 try{
                                      Thread.sleep(1000);
                                 }catch(Exception e){}
                            }
                         };
                         Thread th = new Thread(r);*/
                         //th.start();

                         new Thread(() -> {
                              while(true){
                                   System.out.println("쓰레드의 일3");
                                   try{
                                        Thread.sleep(1000);
                                   }catch(Exception e){}
                              }
                         }).start();
                }
                public static void main(String[] args) {
                     D d = new D();
                     d.use();
                }
           }

'Java > Java' 카테고리의 다른 글

Java 15. NetWork  (0) 2022.03.11
Java 14. IO( Input Output )  (0) 2022.03.11
Java 12. 쓰레드 ( Thread )  (0) 2022.03.11
Java 11. 어노테이션 ( Annotation )  (0) 2022.03.11
Java 10. 내부클래스 ( Inner Class )  (0) 2022.03.10