Consumer:消费型接口
方法:void accept(T t)
dart
private static void test01(){
happy(50000,(money)-> System.out.println("zrx将来月薪"+money+"元"));
}
public static void happy(double money, Consumer<Double> com){
com.accept(money);
}
函数式接口作为方法参数
Supplier :供给型接口
方法:T get();
dart
//产生指定个数个整数,并放入集合中
private static void test02(){
List<Integer> list = getNumList(10,()-> (int)(Math.random()*100));
for (Integer i : list) {
System.out.print(i+" ");
}
}
public static List<Integer> getNumList(int num, Supplier<Integer> sup) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(sup.get());
}
return list;
}
Function<T,R> :函数型接口
方法:R apply(T t);
dart
//用于处理字符串
public static void test03(){
System.out.println("====================");
String s = strHandler("abcdefghi", (str) -> str.substring(2, 4));
System.out.println(s);
String s1 = strHandler("abcdefghi", str -> str.toUpperCase());
System.out.println(s1);
String s2 = strHandler("ABCDEFG", str -> str.toLowerCase());
System.out.println(s2);
String s3 = strHandler("\t\t\t str\t ", str -> str.trim());
System.out.println(s3);
System.out.println("====================");
}
public static String strHandler(String str, Function<String,String> fun){
return fun.apply(str);
}
Predicate :断言型接口
方法:boolean test(T t)
dart
//满足条件的字符串放入集合中
private static void test04() {
List<String> love = Arrays.asList(
"Hello01", "yao", "jing", "yu", "❤", "zhang", "rui", "xu"
);
List<String> list = filterStr(love,str->str.length()<=5);
for (String s : list) {
System.out.print(s+" ");
}
}
public static List<String> filterStr(List<String> list, Predicate<String> pre){
ArrayList<String> list1 = new ArrayList<>();
for (String s : list) {
if(pre.test(s)){
list1.add(s);
}
}
return list1;
}