46.函数式接口
1.概述
@FunctionalInterface public interface FunctionInterface { void show(); }
2.函数式接口作为方法参数
public class Demo { public static void main(String[] args) { startThread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "启动一个线程"); } }); startThread(() -> System.out.println(Thread.currentThread().getName() + "启动一个线程")); } public static void startThread(Runnable runnable) { Thread thread = new Thread(runnable); thread.start(); } }
3.函数式接口作为返回值
public static Comparator<String> getComparator() { // return new Comparator<String>() { // @Override // public int compare(String s1, String s2) { // return s1.length()-s2.length(); // } // }; return (s1,s2)->s1.length()-s2.length(); } // 将下面集合中的字符串按照长度排序 ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("aaaaa"); list.add("aaaaaa"); list.add("aaaa"); System.out.println("排序之前:"+list);//[aaa, aaaaa, aaaaaa, aaaa] Collections.sort(list,getComparator()); System.out.println("排序之后:"+list);//[aaa, aaaa, aaaaa, aaaaaa]
4.常用函数式接口
Supplier接口
public class Demo2 { public static void main(String[] args) { String str = getStr(new Supplier<String>() { @Override public String get() { return "字符串"; } }); System.out.println(str); String str1 = getStr(() -> "字符串"); System.out.println(str1); } public static String getStr(Supplier<String> supplier) { return supplier.get(); } }
public static Integer getMax(Supplier<Integer> supplier) { return supplier.get(); } int []num={15,55,88,33,66}; Integer max = getMax(new Supplier<Integer>() { @Override public Integer get() { int max = num[0]; for (int i = 0; i < num.length; i++) { if (num[i] > max) { max = num[i]; } } return max; } }); Integer max1 = getMax(() -> { int maxValue = num[0]; for (int i = 0; i < num.length; i++) { if (num[i] > maxValue) { maxValue = num[i]; } } return maxValue; }); System.out.println(max); System.out.println(max1);
Consumer接口
public class ConsumerDemo { public static void main(String[] args) { opertorStr("林青霞", s -> System.out.println(s));//林青霞 opertorStr2("周星驰", s -> System.out.println(s), s -> System.out.println(new StringBuilder(s).reverse().toString()));//周星驰驰星周 String s[] = {"林青霞,18", "周星驰,58", "云舒,17"}; opertorStrArr(s, str -> System.out.print("姓名:" + str.split(",")[0]), str -> System.out.println(",年龄:" + str.split(",")[1])); } // 1.定义一个方法消费一个字符串 public static void opertorStr(String s, Consumer<String> consumer) { consumer.accept(s); } // 2.定义一个方法消费两次字符串 public static void opertorStr2(String s, Consumer<String> consumer1, Consumer<String> consumer2) { // consumer1.accept(s); // consumer2.accept(s); consumer1.andThen(consumer2).accept(s); } // 3.消费一个字符串数组 public static void opertorStrArr(String[] s, Consumer<String> consumer1, Consumer<String> consumer2) { // consumer1.accept(s); // consumer2.accept(s); for (String str : s) { consumer1.andThen(consumer2).accept(str); } }
Predicate接口
public class PredicateDemo { public static void main(String[] args) { boolean b1 = checkStr("hello", s -> s.length() > 8); System.out.println(b1); boolean b2 = checkStr2("helloWorld", s -> s.length() > 8, s -> s.length() < 15); System.out.println(b2); } public static boolean checkStr(String s, Predicate<String> predicate) { return predicate.test(s); // return predicate.negate().test(s);//对test结果取非操作 } // 同一个字符串给出两个判断,两个判断结果进行逻辑运算之后返回 public static boolean checkStr2(String s, Predicate<String> predicate1, Predicate<String> predicate2) { // boolean b1= predicate1.test(s); // boolean b2= predicate2.test(s); // return b1&&b2; return predicate1.and(predicate2).test(s); } }
String s[] = {"林青霞,25", "周星驰,55", "成龙,60", "胡歌,30"};
ArrayList<String> strings = checkArr(s, s1 -> s1.split(",")[0].length() > 2, s1 -> Integer.parseInt(s1.split(",")[1]) < 26);
for (String str : strings) {
System.out.println(str);
}
public static ArrayList<String> checkArr(String s[], Predicate<String> predicate1, Predicate<String> predicate2) {
ArrayList<String> strings = new ArrayList<>();
for (String str : s) {
if (predicate1.and(predicate2).test(str)) {
strings.add(str);
}
}
return strings;
}
Funftion接口
public class FunftionDemo { public static void main(String[] args) { convert("100", s -> Integer.parseInt(s)); convert("100", s -> Integer.parseInt(s) + 500, s -> String.valueOf(s)); } public static void convert(String s, Function<String, Integer> function) { Integer apply = function.apply(s); System.out.println(apply); } //Function<String,Integer>:String(泛型1)是apply()方法输入的参数类型,Integer(泛型2)是apply()方法输出的类型 public static void convert(String s, Function<String, Integer> f1, Function<Integer, String> f2) { // Integer apply1 = f1.apply(s); // String apply2 = f2.apply(apply1); // System.out.println(apply2); String s1 = f1.andThen(f2).apply(s);//将f1的apply返回结果作为f2的apply()方法的参数 System.out.println(s1); } }
Function<String, Integer>