函数式接口
概念
有且仅有一个抽象方法的接口。
注意:重写了超类Object类中任意一个public方法的方法并不算接口中的抽象方法。
如何检测一个接口是不是函数式接口
@FunctionalInterface
放在接口定义的上方:如果接口是函数式接口,编译通过;如果不是,编译失败
注意事项
我们自己定义函数式接口的时候,@FunctionalInterface是可选的,就算我不写这个注解,只要保证满足函数 式接口定义的条件,也照样是函数式接口。但是,建议加上该注解
函数式接口作为方法的参数
public class RunnableDemo {
public static void main(String[] args) {
使用该方法方式1:最基本的:创建一个类实现Runnable接口,创建一个该类的实例。把该类作为参数调用该方法。
//方式2:匿名内部类方式去实现。
startThread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+":俺来了!");
}
});
//方式3:lambda表达式
startThread(()->System.out.println(Thread.currentThread().getName()+":俺也来了!"));
}
private static void startThread(Runnable r) {
new Thread(r).start();
}
}
函数式接口作为方法的返回值
public class ComparatorDemo {
public static void main(String[] args) {
//定义集合,存储字符串元素
ArrayList<String> array = new ArrayList<String>();
array.add("cccc");
array.add("aa");
array.add("b");
array.add("ddd");
System.out.println("排序前:" + array);
Collections.sort(array, getComparator());
System.out.println("排序后:" + array);
}
private static Comparator<String> getComparator() {
//匿名内部类的方式实现
// return new Comparator<String>() {
// @Override
// public int compare(String s1, String s2) {
// return s1.length()-s2.length();
// }
// };
//Lambda方式实现
return (s1, s2) -> s1.length() - s2.length();
}
}
常用函数式接口之Supplier
Supplier接口也被称为生产型接口,如果我们指定了接口的泛型是什么类型,那么接口中的get方法就会生产 什么类型的数据供我们使用。
只有一个无参的方法:T get() 按照某种实现逻辑(由Lambda表达式实现)返回一个数据。
public class SupplierDemo {
public static void main(String[] args) {
String s = getString(() -> "林青霞");//其实是()—>{return "林青霞"};省略模式。
System.out.println(s);
Integer i = getInteger(() -> 30);
System.out.println(i);
}
//定义一个方法,返回一个整数数据
private static Integer getInteger(Supplier<Integer> sup) {
return sup.get();
}
//定义一个方法,返回一个字符串数据
private static String getString(Supplier<String> sup) {
return sup.get();
}
}
Supplier接口练习之获取最大值
案例需求
定义一个类(SupplierTest),在类中提供两个方法
一个方法是:int getMax(Supplier sup) 用于返回一个int数组中的最大值
一个方法是主方法,在主方法中调用getMax方法
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, NoSuchFieldException {
//案例需求
//定义一个类(SupplierTest),在类中提供两个方法
//一个方法是:int getMax(Supplier sup) 用于返回一个int数组中的最大值
//一个方法是主方法,在主方法中调用getMax方法
int[] arr = {1,5,3,6,7,8,9,2,3};
//直接使用lambda方式。
int maxElement = getMax(()->{
int max = arr[0];
for (int i = 1; i < arr.length; i++){
if (max < arr[i]){
max = arr[i];
}
}
return max;
});
System.out.println(maxElement);
}
public static int getMax(Supplier<Integer> sup){
return sup.get();
}
常用函数式接口之Consumer
Consumer接口也被称为消费型接口,它消费的数据的数据类型由泛型指定
Consumer:包含两个方法
方法名 说明
void accept(T t) 对给定的参数执行此操作
default Consumer andThen(Consumer after)返回一个组合的Consumer,依次执行此操作,然后执行 after操作
public static void main(String[] args) {
//操作一
operatorString("林青霞", s -> System.out.println(s));
//操作二
operatorString("林青霞", s -> System.out.println(new
StringBuilder(s).reverse().toString()));
System.out.println("--------");
//传入两个操作使用andThen完成
operatorString("林青霞", s -> System.out.println(s), s ->
System.out.println(new StringBuilder(s).reverse().toString()));
}
//定义一个方法,用不同的方式消费同一个字符串数据两次
private static void operatorString(String name, Consumer<String> con1, Consumer<String> con2) {
// con1.accept(name);
// con2.accept(name);
con1.andThen(con2).accept(name);
}
//定义一个方法,消费一个字符串数据
private static void operatorString(String name, Consumer<String> con) {
con.accept(name);
}
Consumer接口练习之按要求打印信息
public static void main(String[] args) {
//Consumer接口练习之按要求打印信息
//案例需求:
//String[] strArray = {"林青霞,30", "张曼玉,35", "王祖贤,33"};
//字符串数组中有多条信息,请按照格式:“姓名:XX,年龄:XX"的格式将信息打印出来
//要求:
//把打印姓名的动作作为第一个Consumer接口的Lambda实例
// 把打印年龄的动作作为第二个Consumer接口的Lambda实例
// 将两个Consumer接口按照顺序组合到一起使用
//1.定义上述数组
String[] strArray = {"林青霞,30", "张曼玉,35", "王祖贤,33"};
//3.调用该方法使用lambda表达式。
printInfo(strArray,str->System.out.print("姓名:" + str.split(",")[0])
,str-> System.out.println("年龄:"+str.split(",")[1]));
}
//2.创建一个方法用返回值为void 参数为字符串数组和两个Consumer<String>。
//函数内部实现遍历字符串数组,并且让两个接口轮流accept
public static void printInfo(String[] stringArray, Consumer<String> cs1, Consumer<String> cs2){
for (String str : stringArray) {
cs1.andThen(cs2).accept(str);
}
}
常用函数式接口之Predicate
Predicate接口通常用于判断参数是否满足指定的条件
方法:
boolean test(T t) 对给定的参数进行判断(判断逻辑由Lambda表达式实现),返回一个布尔值
default Predicate negate() 返回一个逻辑的否定,对应逻辑非
default Predicate and(Predicate other) 返回一个组合判断,对应短路与
default Predicate or(Predicate other) 返回一个组合判断,对应短路或
public static void main(String[] args) {
//boolean test(T t) 对给定的参数进行判断(判断逻辑由Lambda表达式实现),返回一个布尔值
//default Predicate negate() 返回一个逻辑的否定,对应逻辑非
//default Predicate and(Predicate other) 返回一个组合判断,对应短路与
//default Predicate or(Predicate other) 返回一个组合判断,对应短路或
boolean b1 = checkString("hello", s -> s.length() > 8);
System.out.println(b1);
boolean b2 = checkString("helloWorld",s -> s.length() > 8);
System.out.println(b2);
}
private static boolean checkString(String s, Predicate<String> pre) {
// return !pre.test(s);
return pre.negate().test(s);//首先是为了的到一个布尔值,先调用negate()加了一个非,然后调用了test方法,判断参数,
// 具体用lambda表达式实现,来返回一个布尔值。
}
public class PredicateDemo02 {
public static void main(String[] args) {
boolean b1 = checkString("hello", s -> s.length() > 8);
System.out.println(b1);
boolean b2 = checkString("helloworld", s -> s.length() > 8);
System.out.println(b2);
boolean b3 = checkString("hello",s -> s.length() > 8, s -> s.length() < 15);
System.out.println(b3);
boolean b4 = checkString("helloworld",s -> s.length() > 8, s -> s.length() < 15);
System.out.println(b4);
}
//同一个字符串给出两个不同的判断条件,最后把这两个判断的结果做逻辑或运算的结果作为最终的结果
private static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2) {
return pre1.or(pre2).test(s);
}
//判断给定的字符串是否满足要求
private static boolean checkString(String s, Predicate<String> pre) {
return pre.test(s);
}
}
Predicate接口练习之筛选满足条件数据
public class PredicateTest {
public static void main(String[] args) {
String[] strArray = {"林青霞,30", "柳岩,34", "张曼玉,35", "貂蝉,31", "王祖 贤,33"};
ArrayList<String> array = myFilter(strArray, s -> s.split(",")[0].length() > 2,
s -> Integer.parseInt(s.split(",")[1]) > 33);
for (String str : array) {
System.out.println(str);
}
}
//通过Predicate接口的拼装将符合要求的字符串筛选到集合ArrayList中
private static ArrayList<String> myFilter(String[] strArray, Predicate<String> pre1, Predicate<String> pre2) {
//定义一个集合
ArrayList<String> array = new ArrayList<String>();
//遍历数组
for (String str : strArray) {
if (pre1.and(pre2).test(str)) {
array.add(str);
}
}
return array;
}
}