메서드 레퍼런스는 람다식과 함께 사용될 수 있다.
ClassName::MethodName
형식
//람다식
Consumer<String> func = text -> System.out.println(text);
func.accept("Hello");
//메서드 레퍼런스 표현
Consumer<String> func = System::println;
sunc.accept("Hello");
public class Example {
public static void main(String[] args) {
//람다식 사용
Executable exe = text -> Printer.printSomething(text);
exe.doSomething("do something");
//메서드 레퍼런스
Executable exe2 = Printer::printSomething;
exe2.doSomething("do something");
}
interface Executable {
void doSomething(String text);
}
public static class Printer {
static void printSomething(String text) {
System.out.println(text);
}
}
}
Custom 인터페이스인 Executable를 Java에서 기본으로 제공하는 함수형 인터페이스인 Consumer로 대체
Consumer<String>
은 String 1개를 인자로 받아 void를 리턴하는 메소드를 갖고 있는 인터페이스
import java.util.function.Consumer;
public class Example {
public static void main(String[] args) {
Consumer<String> exe = text -> Printer.printSomething(text);
exe.accept("do something");
Consumer<String> exe2 = Printer::printSomething;
exe2.accept("do something");
}
public static class Printer {
static void printSomething(String text) {
System.out.println(text);
}
}
}
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> companies = Arrays.asList("google", "apple", "google", "apple", "samsung");
// 1. lambda expression
companies.stream().forEach(company -> System.out.println(company));
// 2. static method reference
companies.stream().forEach(System.out::println);
}
}
Instance 메소드 레퍼런스는 static 메소드가 아닌, 객체의 멤버 메소드를 메소드 레퍼런스로 사용하는 것을 의미합니다.
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
//람다식 사용
List<Company> companies = Arrays.asList(new Company("google"),
new Company("apple"), new Company("samsung"));
companies.stream().forEach(company -> company.printName());
}
//메서드 레퍼런스 사용
List<Company> companies =
Arrays.asList(new Company("google"), new Company("apple"), new Company("samsung"));
companies.stream().forEach(Company::printName);
public static class Company {
String name;
public Company(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
}
}
문자열의 길이를 리턴하는 String.length() 함수를 메소드 레퍼런스로 표현하면 String::length가 됩니다
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> companies = Arrays.asList("google", "apple", "google", "apple", "samsung");
companies.stream()
.mapToInt(String::length)
// .mapToInt(company -> company.length()) /* Lamda expression */
.forEach(System.out::println);
}
}
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
//람다식 사용
List<String> companies = Arrays.asList("google", "apple", "google", "apple", "samsung");
companies.stream()
.map(name -> new Company(name))
.forEach(company -> company.printName());
//메서드 레퍼런스 사용
List<String> companies = Arrays.asList("google", "apple", "google", "apple", "samsung");
companies.stream()
.map(Company::new)
.forEach(Company::printName);
}
public static class Company {
String name;
public Company(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
}
}