92 java8新特性_3 _函数式接口
函数式接口
-
函数式接口:
- 如果一个接口只有一个抽象方法,则该接口称之为函数式接口,函数式接口可以使用Lambda表达式,Lambda表达式会被匹配到这个抽象方法上。
- 只有函数式接口才能使用lambda表达式
-
@FunctionalInterface 注解检测接口是否符合函数式接口。
代码1:
package com.wlw.chapter13_java8.demo01;
/**
* 函数式接口
*/
@FunctionalInterface //检测接口是否符合函数式接口。
public interface Usb {
void service();
}
package com.wlw.chapter13_java8.demo01;
public class Demo {
public static void main(String[] args) {
//匿名内部类
Usb mouse = new Usb() {
@Override
public void service() {
System.out.println("鼠标开始工作了.....");
}
};
//lambda表达式
Usb fan = ()->{
System.out.println("风扇开始工作了........");
};
//Usb fan2 = ()-> System.out.println("风扇开始工作了2........");
run(mouse);
run(fan);
}
public static void run(Usb usb){
usb.service();
}
}
/*执行结果:
鼠标开始工作了.....
风扇开始工作了........
*/
常见的函数式接口
函数式接口 | 方法参数类型 | 方法返回类型 | 说明 |
---|---|---|---|
Consumer< T> 消费型接口 | T | void | void accept(T t); 对类型为T的对象应用操作 |
Supplier< T> 供给型接口 | 无 | T | T get(); 返回类型为T的对象 |
Function<T,R> 函数型接口 | T | R | R apply(T t); 对类型为T的对象应用操作,并返回类型为R类型的对象。 |
Predicate< T> 断言型接口 | T | boolean | boolean test(T t); 确定类型为T的对象是否满足条件,并返回boolean类型。 |
代码1:
package com.wlw.chapter13_java8.demo01;
import com.wlw.chapter9_thread.casedemo04.Consume;
import java.util.Arrays;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class Demo {
public static void main(String[] args) {
//匿名内部类
Consumer<Double> consumer = new Consumer<Double>() {
@Override
public void accept(Double money) {
System.out.println("consumer唱歌消费了:"+money);
}
};
//lambda表达式 ,如果形参只有1个,()可以省略,只需要参数的名称即可,
Consumer<Double> consumer1 = t ->{
System.out.println("consumer1唱歌消费了:"+ t);
};
happy(consumer,1000.0);
happy(consumer1,1000.1);
happy(t-> System.out.println("consumer3吃饭消费了:"+ t),2000.0);
System.out.println("=====================================================================");
//匿名内部类
Supplier<Integer> supplier = new Supplier<Integer>() {
@Override
public Integer get() {
return new Random().nextInt(100);
}
};
//lambda表达式
Supplier<Integer> supplier1 = ()-> {
return new Random().nextInt(1000);
};
int[] nums = getNums(supplier, 5);
System.out.println(Arrays.toString(nums));
int[] nums1 = getNums(supplier1, 5);
System.out.println(Arrays.toString(nums1));
int[] nums2 = getNums(()->new Random().nextInt(10), 5);
System.out.println(Arrays.toString(nums2));
}
//Consumer<T>消费型接口
public static void happy(Consumer<Double> consumer, Double money){
consumer.accept(money);
}
//Supplier<T>供给型接口
public static int[] getNums(Supplier<Integer> supplier,int count){
int[] arr = new int[count];
for (int i = 0; i < count; i++) {
arr[i] = supplier.get();
}
return arr;
}
}
/* 执行结果:
consumer唱歌消费了:1000.0
consumer1唱歌消费了:1000.1
consumer3吃饭消费了:2000.0
=====================================================================
[45, 71, 69, 5, 54]
[604, 149, 414, 618, 943]
[6, 1, 8, 3, 4]
*/
代码2:
package com.wlw.chapter13_java8.demo01;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
public class Demo02 {
public static void main(String[] args) {
//匿名内部类
Function<String,String> function = new Function<String, String>() {
@Override
public String apply(String s) {
return s.toUpperCase();
}
};
//lambda表达式
Function<String,String> function1 = (s)-> {
return s.trim();
};
String result = handlerString(function, "hello");
System.out.println(result);
String result1 = handlerString(function1, " hello zhangsan ");
System.out.println(result1);
String result2 = handlerString(s -> s.toLowerCase(),"HELLOWORLD");
System.out.println(result2);
System.out.println("===============================================================");
//匿名内部类
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.startsWith("zhang");
}
};
List<String> list = new ArrayList<>();
list.add("zhangwuji");
list.add("zhangsan");
list.add("zhangxiaofan");
list.add("wangwu");
list.add("lisi");
//lambda表达式
Predicate<String> predicate1 = (s)->{
return s.startsWith("zhang");
};
List<String> resultlist = filterNames(predicate, list);
System.out.println(resultlist);
List<String> resultlist1 = filterNames(predicate1, list);
System.out.println(resultlist1);
List<String> resultlist2 = filterNames(s -> s.length()>7, list);
System.out.println(resultlist2);
}
//Function<T,R>函数型接口 R apply(T t);
public static String handlerString(Function<String,String> function,String str){
return function.apply(str);
}
//Predicate<T>断言型接口
public static List<String> filterNames(Predicate<String> predicate,List<String> list){
List<String> list1 = new ArrayList<>();
for (String result : list){
if(predicate.test(result)){
list1.add(result);
}
}
return list1;
}
}
/*执行结果:
HELLO
hello zhangsan
helloworld
===============================================================
[zhangwuji, zhangsan, zhangxiaofan]
[zhangwuji, zhangsan, zhangxiaofan]
[zhangwuji, zhangsan, zhangxiaofan]
*/