Lambda

๊ธฐ์กด ์ž๋ฐ”์—์„œ ๋ชจ๋˜ ์ž๋ฐ”๋กœ ๋„˜์–ด์˜ค๋ฉด์„œ ์ธ์ž์— ๋กœ์ง์„ ๋„ฃ๋Š” ๋ฐฉ์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค

of.ifPresent(junny -> System.out.println("HELLO"));

์ด๋Ÿฐ๊ฒƒ์„ ๋žŒ๋‹ค์‹ ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค

ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค

ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์ด ๋– ์˜ค๋ฅด๋ฉด์„œ,

์ž๋ฐ” ์ง„ํ˜•์—์„œ๋„ ์ด์™€ ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์„ ์–ธํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์„ ์ถ”๊ตฌํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋Š˜์–ด๋‚˜๊ณ  ์žˆ๋Š” ์ถ”์„ธ ์ž…๋‹ˆ๋‹ค

๊ทธ์ค‘, ์ด ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ํ‰๋‚ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค

ex)

@FunctionalInterface
public interface JunnyFunction {
    void accept();
}
public class JunnyService {
    public JunnyService(JunnyFunction fuction) {}
}
public static void main(String[] args) {
  JunnyService junnyService = new JunnyService(() -> System.out.println("Hello World"));
}

ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฒ•์€ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•œ๋ฐ, ์ธํ„ฐํŽ˜์ด์Šค์— ๋ช…์„ธ๊ฐ€ ํ•œ๊ฐœ, ์ฆ‰ ๋ฉ”์†Œ๋“œ๋ฅผ ํ•˜๋‚˜๋งŒ ์„ ์–ธํ•œ๋‹ค๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@FunctionalInterface
public interface Sample {
    void execute();
}

Sample sample = () -> 
@FunctionalInterface : 2๊ฐœ์ด์ƒ์˜ ์„ ์–ธ์„ ๋ฐฉ์ง€ํ•œ๋‹ค  

์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” API

์ธํ„ฐํŽ˜์ด์ŠคDescripterMethod

Predicate

์ธ์ž ํ•˜๋‚˜๋ฅผ ๋ฐ›์•„์„œ boolea ๋ฆฌํ„ด

boolean test(T t)

Consumer

Consumer ๋Š” ์ธ์ž ํ•˜๋‚˜๋ฅผ ๋ฐ›๊ณ  void

void accept(T t)

Supplier

์•„๋ฌด๋Ÿฐ ์ธ์ž๋ฅผ ๋ฐ›์ง€ ์•Š๊ณ  T ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด

T get()

Function<T, R>

T ํƒ€์ž… ์ธ์ž๋ฅผ ๋ฐ›์•„์„œ R ํƒ€์ž…์„ ๋ฆฌํ„ด

R apply(T t)

Comparator

T ํƒ€์ž… ์ธ์ž ๋‘๊ฐœ๋ฅผ ๋ฐ›์•„์„œ intํƒ€์ž…์„ ๋ฆฌํ„ด

int compare(T o1, T o2)

Runnable

์•„๋ฌด๋Ÿฐ ๊ฐ์ฒด๋ฅผ ๋ฐ›์ง€ ์•Š๊ณ  void

void run()

Callable

์•„๋ฌด๋Ÿฐ ์ธ์ž๋ฅผ ๋ฐ›์ง€ ์•Š๊ณ  T๋ฆฌํ„ด

V call()

์ด ์™ธ์—๋„

๋“ฑ ์‘์šฉํ•ด์„œ ์ •์˜๋œ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ์œผ๋‹ˆ,

๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํ•จ์ˆ˜ํ˜•์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ• ๋งŒํ•œ ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค

Last updated