함수형 인터페이스
- 함수형 인터페이스
- 단 하나의 추상 메서드를 갖는 인터페이스이다.
- Java8 부터 함수형 인터페이스를 컴파일 시점에도 확인할 수 있도록 @FunctionalInterface 애노테이션도 제공한다. (없어도 문제는 되지 않는다.)
- 람다는 함수형 인터페이스를 간단히 하기 위해서 만들어졌다.
- 파라미터 a가 여러 함수형 인터페이스를 거쳐 최종적인 값 b의 형태로 변경할 수 있다.
- 함수형 인터페이스 예시
1. SearchOption.java
package functional;
public enum SearchOption {
Location, Gender
}
2. Gender.java
package functional;
public enum Gender {
Male, Female
}
3. SearchFilter.java
package functional;
public interface SearchFilter {
boolean isMatched(Customer customer);
}
4. Customer.java
package functional;
public class Customer {
private String id;
private String location;
private Gender gender;
private int age;
public Customer(String id, String location, Gender gender, int age) {
super();
this.id = id;
this.location = location;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
return builder.append(id).append(" : ").
append(location).append(" : ").
append(gender).append(" : ").
append(age).toString();
}
}
5.CustomerService
package functional;
import java.util.ArrayList;
import java.util.List;
public class CustomerService {
private List<Customer> customers;
public CustomerService() {
this.customers = new ArrayList<>();
}
public void addCustomer(Customer newCustomer) {
this.customers.add(newCustomer);
}
public List<Customer> searchCustomerByLocation(String location) {
List<Customer> foundCustomers = new ArrayList<>();
for(Customer customer : customers) {
if(customer.getLocation().equals(location)) {
foundCustomers.add(customer);
}
}
return foundCustomers;
}
public List<Customer> searchCustomerBy(SearchOption searchOption, String searchValue){
List<Customer> foundCustomers = new ArrayList<>();
for(Customer customer : customers) {
if(searchOption.equals(SearchOption.Location)) {
if(customer.getLocation().equals(searchValue)) {
foundCustomers.add(customer);
}
}else if(searchOption.equals(SearchOption.Gender)) {
if(customer.getGender().equals(searchValue)) {
foundCustomers.add(customer);
}
}
}
return foundCustomers;
}
public List<Customer> searchCustomers(SearchFilter filter){
List<Customer> foundCustomers = new ArrayList<>();
for(Customer customer : customers) {
if(filter.isMatched(customer)) {
foundCustomers.add(customer);
}
}
return foundCustomers;
}
}
6.FunctionalAssist
package functional;
import java.util.List;
public class FunctionalAssist {
public static void main(String[] args) {
CustomerService service = new CustomerService();
initData(service);
List<Customer> list = service.searchCustomerByLocation("kangnam");
list = service.searchCustomers(new SearchFilter() {
@Override
public boolean isMatched(Customer customer) {
if(customer.getLocation().equals("Sungsu")) {
return true;
}else {
return false;
}
}
});
for(Customer cu : list) {
System.out.println(cu);
}
System.out.println();
list = service.searchCustomers(customer -> customer.getGender().equals(Gender.Male));
for(Customer cu : list) {
System.out.println(cu);
}
System.out.println();
list = service.searchCustomers(customer -> customer.getGender().equals(Gender.Female) && customer.getAge() > 25);
for(Customer cu : list) {
System.out.println(cu);
}
System.out.println();
}
private static void initData(CustomerService service) {
service.addCustomer(new Customer("CUS01","Sungsu",Gender.Male,30));
service.addCustomer(new Customer("CUS02","ddoksum",Gender.Female,55));
service.addCustomer(new Customer("CUS03","hanyang",Gender.Female,13));
service.addCustomer(new Customer("CUS04","Sungsu",Gender.Male,27));
service.addCustomer(new Customer("CUS05","Sungsu",Gender.Female,61));
service.addCustomer(new Customer("CUS06","kangnam",Gender.Male,28));
service.addCustomer(new Customer("CUS07","hanyang",Gender.Male,25));
service.addCustomer(new Customer("CUS08","kangnam",Gender.Female,45));
service.addCustomer(new Customer("CUS09","suwon",Gender.Female,42));
service.addCustomer(new Customer("CUS10","kangnam",Gender.Male,58));
}
}
출처