Function기능적 인터페이스
public interface Function<T, R> {
/**
* Applies this function to the given argument
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
// 인터페이스가 작동하는 한 람다 표현식을 사용할 수 있습니다.
public class test1 {
public static void main(String[] args) {
Function function = new Function<String, String>() {
@Override
public String apply(String str) {
return str;
}
};
//lambda
Function<String,String> function = (str)->{return str;};
System.out.println(function.apply("asdf"));
}
}
술어 판단 인터페이스
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
}
public class test2 {
public static void main(String[] args) {
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.isEmpty();
}
};
Predicate<String> predicate = (string)->{
return string.isEmpty();
};
System.out.println(predicate.test("hello")); //false
}
}
소비자 소비자 인터페이스
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
* @param t the input argument
*/
void accept(T t);
}
public class test3 {
public static void main(String[] args) {
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
Consumer<String> consumer = (string)->{
System.out.println(string);
};
consumer.accept("hello");
}
}
공급업체 공급 유형 인터페이스
public interface Supplier<T> {
/**
* Gets a result.
* @return a result
*/
T get();
}
public class test4 {
public static void main(String[] args) {
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return "1024";
}
};
Supplier<String> supplier = ()->{
return "1024";
};
System.out.println(supplier.get());
}
}
요약
public class ConsumerPractice {
public static void main(String[] args) {
Consumer<String> a=b-> System.out.println(b);
a.accept("hello");
Function<Integer,String> ad=b->b.toString();
System.out.println(ad.apply(11));
Predicate<String> ac=b->b.isEmpty();
System.out.println(ac.test("hello"));
Supplier<String> ae= ()-> "a";
System.out.println(ae.get());
}
}