1、Consumer:消费型接口,有参无返回值
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}
第一种写法:
Consumer<String> qqq=new Consumer<String>() {
@Override
public void accept(String s) {
}
};
第二种写法:
Consumer<String> stringConsumer = str -> {
System.out.println(str);
};
//还可以简化成下面这样
Consumer<String> stringConsumer = str -> System.out.println(str);
作用:给传进来的参数做自定义处理,无论你怎么处理都可以,但是并没有返回值
2、Supplier:供给型接口
*/
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Supplier<String> stringSupplier = () -> "hello";
测试:
@Test
public void test2() {
Supplier<String> stringSupplier = () -> "hello";
String value = getValue(stringSupplier);
System.out.println(value);
}
/**
* Supplier<T> 供给型接口
*
* @param sup
* @return
*/
public String getValue(Supplier<String> sup) {
return sup.get();
}
作用:只有返回值出来,但是并没有参数
3、Function函数接口:
@FunctionalInterface
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);
}
表达式:
Function<Long, Long> longLongFunction = (x) -> {
return x + 200L;
};
測試:
@Test
public void test3() {
Function<Long, Long> longLongFunction = (x) -> {
return x + 200L;
};
Long result = changeNum(100L, longLongFunction);
System.out.println(result);
}
/**
* Function<T,R> 函数式接口
*
* @param num
* @param fun
* @return
*/
public Long changeNum(Long num, Function<Long, Long> fun) {
return fun.apply(num);
}
作用:当你调用这个对象的apply方式时,它能帮你完成根据传入的参数,做某些操作,然后在输出一个对象的操作!
4、Predicate:断言型接口,有参有返回值,返回值是boolean类型
@FunctionalInterface
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);
}
表达式:
Predicate<String> stringPredicate = (str) -> str.length() > 5;
测试:
// Predicate《T》: 断言型接口,有参有返回值,返回值是boolean类型
public void test4() {
Predicate<String> stringPredicate = (str) -> str.length() > 5;
boolean result = changeBoolean("hello", stringPredicate);
System.out.println(result);
}
/**
* Predicate<T> 断言型接口
*
* @param str
* @param pre
* @return
*/
public boolean changeBoolean(String str, Predicate<String> pre) {
return pre.test(str);
}
作用:有一个输入参数,且有一个返回值,但是返回值是个boolean类型的
5、Consumer接口还有个方法:andThen
表达式:
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello ");
Consumer<StringBuilder> consumer = (str) -> str.append("Jack!");
Consumer<StringBuilder> consumer1 = (str) -> str.append(" Bob!");
Consumer<StringBuilder> stringBuilderConsumer = consumer.andThen(consumer1);
stringBuilderConsumer.accept(sb);
System.out.println(sb.toString()); // Hello Jack! Bob!
//accept 其实就是在你传的参数后面做某个操作,
//andThen:由两个消费者组成的,先是掉第一个消费者的accept 再掉第二个消费者的accept
}
作用:先是掉了accept方法,后续在再此基础上,再做一遍accept,相当于做了两边,只不过有个先后顺序。