JAVA/JAVA8
Supplier Functional Interface
최-코드
2024. 8. 13. 16:26
- 입력을 받지 않고, 출력만을 하는 함수형 인터페이스이다. Consumer와 정반대이다.
- 주로 객체 생성이나 값을 반환하는데 사용된다.
- get() 메소드를 구현해줘야 한다.
public class SupplierExample {
public static void main(String[] args) {
Supplier<Student> studentSupplier = () -> new Student("John",2,3.8, "male", List.of("swiming, basketball"));
Student student = studentSupplier.get();
System.out.println(student);
Supplier<List<Student>> studentListSupplier = () -> StudentDataBase.getAllStudents();
List<Student> studentList = studentListSupplier.get();
studentList.forEach((stu) -> System.out.println(stu));
}
}