Software Engineering/Java, JSP

[Java] 제5장 제네릭과 람다식

iseop 2023. 4. 30. 19:16   인쇄용 버전

1. 단원 요약

  • 다양한 타입이나 객체에 대해 동작하게 만든 클래스를 제네릭 클래스 또는 제네릭이라고 부른다.
  • 제네릭을 정의할 때 다양한 타입 파라미터를 선언하고, 사용할 때 필요한 타입을 지정한다.
    • 컴파일러를 통한 타입 검사가 가능해진다.
    • 명시적 형변환이 불필요해진다.
  • 자료형 매개변수(타입 파라미터)는 클래스 식별자 뒤에 <>로 표시한다.
    • 타입 파라미터는 필드의 타입이나 반환형으로 사용된다.
    • 타입 매개변수 없이 사용되는 제네릭 타입을 로우(raw)타입이라 한다.
    • Raw 타입 클래스 내부에서 사용되는 모든 타입이 java.lang.Object로 간주되어 실행된다.
  • 람다식(lambda expression)이란 파라미터를 입력받아 결과값을 반환하는 코드이다.
  • 람다식은 메소드와 비슷하나, 이름이 없고, 메소드 내부에 존재할 수 있다.
  • 람다식은 하나의 메소드만을 가지는 인터페이스 타입인 변수에 저장될 수 있다.
    • Java 8에는 이러한 인터페이스들이 java.util.function 패키지에 존재한다.
    • 이는 Java 8부터 도입된 함수적 프로그래밍을 위한 패키지이다.

java.util.ArrayList.forEach()로부터 값을 받아 출력하는 람다식

import java.util.ArrayList;

public class App {
  public static void main(String[] args) {
    ArrayList<Integer> intList = new ArrayList<Integer>();
    intList.add(1);
    intList.add(2);
    intList.add(3);
    intList.forEach( (x) -> { System.out.println(x); } );
  }
}

java.util.function.Consumer에 저장되어 사용되는 람다식

* Consumer 인터페이스는 메소드가 한 개인 single-method interface이다.

import java.util.ArrayList;
import java.util.function.Consumer;

public class App {
  public static void main(String[] args) {
    ArrayList<Integer> intList = new ArrayList<Integer>();
    intList.add(1);
    intList.add(2);
    intList.add(3);
    Consumer<Integer> lambdaExpression = (n) -> { System.out.println(n); };
    numbers.forEach( lambdaExpression );
  }
}

메소드 내부에서 사용되는 람다식

* 어떤 메소드 내부에서 람다식을 사용하기 위해서는 single-interface method를 parameter로 받아야 한다.

interface CustomStringFunction {
  String execute(String string);
}

public class App {

  public static void main(String[] args) {
    CustomStringFunction toUpper = (x) -> x.toUpperCase();
    CustomStringFunction toLower = (x) -> x.toLowerCase();
    printWithLambda("abcde", toUpper);
    printWithLambda("ABCDE", toLower);
  }
  
  public static void printWithLambda(String s, CustomStringFunction csf) {
    System.out.println(csf.execute(s));
  }
  
}

 

2. 연습문제 요약

  • 제네릭인 ArrayList를 선언과 동시에 String을 저장하도록 초기화하라.
    • List<String> strList = new ArrayList<String>();
  • 제네릭을 사용할 때는 type parameter로 기본 자료형을 사용할 수 없다.
  • Executable exec = new Executable() { public void execute(){} };를 람다식으로 표현하라.
    • Executable exec = () -> {};
  • java.util.function.Consumer는 accept()메소드만을 가진다.
    • 람다식을 대입하여 원하는 계산을 수행하는 함수처럼 사용할 수 있다.

https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html

 

Consumer (Java Platform SE 8 )

andThen default Consumer  andThen(Consumer  after) Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed op

docs.oracle.com