四大內(nèi)置核心函數(shù)式接口
Java內(nèi)置的函數(shù)式接口是為了方便開(kāi)發(fā)者使用Lambda表達(dá)式,對(duì)于應(yīng)對(duì)大部分函數(shù)式接口的使用而提出的。有了這些內(nèi)置接口,程序員不用自己在代碼中去定義,就能完成大部分工作需求。
- Consumer<T>:消費(fèi)型接口(無(wú)返回值,有一個(gè)泛型的輸入?yún)?shù))
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
- Supplier<T>:供給型接口(無(wú)輸入?yún)?shù),有一個(gè)泛型的返回值)
@FunctionalInterface
public interface Supplier<T> {
T get();
}
- Function<T,R>:函數(shù)型接口(有一個(gè)泛型的輸入?yún)?shù)和一個(gè)泛型的返回值)
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
- Predicate<T>:斷言型(判斷型)接口:(有一個(gè)泛型的輸入?yún)?shù),返回一個(gè)bool值)
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
從上面四個(gè)接口的源碼中可以看到,接口中不一定只有一個(gè)方法的才是函數(shù)式接口,一個(gè)接口是函數(shù)接口的充要條件是有且僅有一個(gè)抽象方法。接口中的其他方法可以通過(guò)加上default關(guān)鍵字提供默認(rèn)實(shí)現(xiàn),或者是一個(gè)static修飾的靜態(tài)方法。
四大內(nèi)置核心函數(shù)式接口使用方法。
public class TestLambda2 {
//Consumer<T>消費(fèi)型接口:(無(wú)返回值,有一個(gè)泛型的輸入?yún)?shù))
@Test
public void test1() {
happy(10000, money -> System.out.println("發(fā)了" + money + "元工資,開(kāi)心啊"));
}
//處理一個(gè)數(shù)據(jù)
public void happy(double money, Consumer<Double> consumer) {
consumer.accept(money);
}
//供給型接口(無(wú)輸入?yún)?shù),有一個(gè)泛型的返回值)
@Test
public void test2(){
getNumList(10, () -> (int) (Math.random() * 100)).forEach(System.out::println);
}
//獲取一個(gè)整數(shù)集合
public List<Integer> getNumList(int num, Supplier<Integer> supplier) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(supplier.get());
}
return list;
}
//Function<T,R>函數(shù)型接口(有一個(gè)泛型的輸入?yún)?shù)和一個(gè)泛型的返回值)
@Test
public void test3(){
System.out.println(strHandler("\t\n " + " 你好?。。?!哈哈 \r\n\t", String::trim));
}
//處理字符串后返回
public String strHandler(String str, Function<String, String> function) {
return function.apply(str);
}
//Predict<T> 斷言型(判斷型)接口:(有一個(gè)泛型的輸入?yún)?shù),返回一個(gè)bool值)
@Test
public void test4(){
List<String> list = Arrays.asList("Hello","what","is","your","name");
filterStr(list, s -> s.length() > 3).forEach(System.out::println);
}
//將滿足條件的字符串,放入集合中
public List<String> filterStr(List<String> list, Predicate<String> predicate) {
List<String> strings = new ArrayList<>();
list.forEach(s -> {
if (predicate.test(s)) {
strings.add(s);
}
});
return strings;
}
}
上述代碼中使用了方法引用。即需要提供一個(gè)函數(shù)式接口的地方,不僅可以使用Lambda表達(dá)式,還可以使用函數(shù)的引用,只需要該函數(shù)的輸入?yún)?shù)和返回值與函數(shù)式接口中的抽象方法一致即可。例如上述代碼中的String::trim以及System.out::println。
擴(kuò)展接口
如果上述四大接口不能滿足需求,還有其他一些內(nèi)置接口可以選用,具體如下,就不一一列舉了其使用方法了。

其他函數(shù)式接口