一、Consumer 接口
1、接口说明
Consumer 接口是消费性接口,无返回值。Java8 中对 Consumer 的定义如下所示。
@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); };
}
}
2、使用示例
public void handlerConsumer(Integer number, Consumer<Integer> consumer){
consumer.accept(number);
}
@Test
public void test1(){
this.handlerConsumer(10000, (i) -> System.out.println(i));
}
二、Supplier 接口
1、接口说明
Supplier 接口是供给型接口,有返回值,Java8 中对 Supplier 接口的定义如下所示。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
2、使用示例
public List<Integer> getNumberList(int num, Supplier<Integer> supplier){
List<Integer> list = new ArrayList<>();
for(int i = 0; i < num; i++){
list.add(supplier.get())
}
return list;
}
@Test
public void test2(){
List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100));
numberList.stream().forEach(System.out::println);
}
三、Function 接口
1、接口说明
Function 接口是函数型接口,有返回值,Java8 中对 Function 接口的定义如下所示。
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
/**
* 先执行参数(即也是一个Function)的,再执行调用者(同样是一个Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* 先执行调用者,再执行参数,和compose相反。
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* 返回当前正在执行的方法
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
2、使用示例
public String handlerString(String str, Function<String, String> func){
return func.apply(str);
}
@Test
public void test3(){
String str = this.handlerString("binghe", (s) -> s.toUpperCase());
System.out.println(str);
}
3、关于andThen的使用
Function<Integer, Integer> functionA = i -> i * 2;
Function<Integer, String> functionB = i -> "-" + i;
functionA.andThen(functionB).apply(2)
输出结果是:-4
等价于下面的functionC
Function<Integer, Integer> functionA = i -> i * 2;
Function<Integer, String> functionB = i -> "-" + i;
Function<Integer, String> functionC = t -> functionB.apply(functionA.apply(t));
输出结果也是:-4
4、对比下几个方法的使用
public static void main(String[] args) {
Function<Integer, Integer> times2 = i -> i*2;
Function<Integer, Integer> squared = i -> i*i;
//8
System.out.println(times2.apply(4));
//16
System.out.println(squared.apply(4));
//32 先4×4然后16×2,先执行apply(4),在times2的apply(16),先执行参数,再执行调用者。
System.out.println(times2.compose(squared).apply(4));
//64 先4×2,然后8×8,先执行times2的函数,在执行squared的函数。
System.out.println(times2.andThen(squared).apply(4));
//16
System.out.println(Function.identity().compose(squared).apply(4));
}
四、Predicate 接口
1、接口说明
Predicate 接口是断言型接口,返回值类型为 boolean,Java8 中对 Predicate 接口的定义如下所示。
@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);
}
}
2、使用示例
public List<String> filterString(List<String> list, Predicate<String> predicate){
List<String> strList = new ArrayList<>();
for(String str : list){
if(predicate.test(str)){
strList.add(str);
}
}
return strList;
}
@Test
public void test4(){
List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World");
List<String> strList = this.filterString(list, (s) -> s.length() >= 5);
strList.stream().forEach(System.out::println);
}
网友评论