본문 바로가기

Web/JAVA

[Java] Interface Supplier<T>

728x90

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/Supplier.html

 

Supplier (Java SE 11 & JDK 11 )

 

docs.oracle.com

 

 

 
  •  

Module java.base

Interface Supplier<T>

  • Type Parameters:T - the type of results supplied by this supplierAll Known Subinterfaces:ServiceLoader.Provider<S>All Known Implementing Classes:GuardingDynamicLinkerExporterFunctional Interface:This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
    @FunctionalInterface
    public interface Supplier<T>
    Represents a supplier of results.

    There is no requirement that a new or distinct result be returned each time the supplier is invoked.

    This is a functional interface whose functional method is get().

    Since:1.8
    • Method Detail
      • get
        Gets a result.
        Returns:a result
      • T get()

 

 

 

 

Supplier<T> 함수형 인터페이스 추상메소드는 

매개변수를 받지않고 단순하게 무엇인가를 반환만 해준다

 

 

쉽게 말해서 그냥 Supplier<T> 는 T get() -> T만을 리턴한다 이게 다임

제레릭 타입은 String이고, get() 을 이용하여 LazyEvaluation이 가능하다 -> 불펼요한 연산이 없다는 뜻

 

 

 

 

다른 함수형 인터페이스 (다 아님 몇개만 가져온거임)

 

 

Function<T, R>

R apply(T t);

-> T를 받은 뒤, R로 리턴시킨다

람다식 : T -> R 

 

Consumer<T>

void accept<T t>;

-> T를 받아 처리 후 리턴하지는 않는다

람다식 : T -> void

 

Predicate<T>

boolean test(T t);

-> T를 받아 처리 후 Boolean 으로 리턴한다

람다식 : T -> boolean

 

Supplier<T>

T get();

-> T를 받아 이를 리턴시킨다 (LazyEvaluation)

람다식 : () -> T

 

Comparator<T>

int compare(T t1, T t2);

-> T를 두개 받은 뒤, int타입을 리턴한다 

람다식 : (T, T) -> int 

 

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Runnable

abstract void run();

-> 실행만 한다

람다식 : () -> void

 

 

@FunctionalInterface
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

Callable(V)

V call() throws Exception;

-> 받은 객체를 리턴한다 (Runnable과 비슷하게 호출가능한지에 대해 체크한다 생각하면 됨)

람다식 : () -> T

728x90

'Web > JAVA' 카테고리의 다른 글

[자료구조] DFS, BFS  (0) 2023.04.17
[Java] Super  (0) 2023.03.23
[Java] this  (0) 2023.03.22
[IntelliJ Javadocs] Javadocs 문서 만들기  (0) 2023.03.18
[Java] try-with-resources  (0) 2023.02.25