- 左侧:Lambda表达式的参数列表
- 右侧:Lambda表达式中所需执行的功能,即Lambda体
- 只有一个抽象方法的接口,称为函数式接口
- 语法格式1:无参数,无返回值
- ()->log.info("hello lambda");
- 语法格式2:有一个参数,无返回值
- (x)-> System.out.println(x);
- 语法格式3:若只有一个参数,小括号可以省略不写
- 语法格式4:有两个以上的参数,有返回值,并且Lambda体中有多条语句
- Comparator com = (x,y) ->{ System.out.println("函数式接口"); return Integer.compare(x,y); };
- 语法格式5:若Lambda体中只有一条语句,return和大括号都可以省略不写
- 语法格式6:Lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出数据类型,即"类型推断"
- 二、Lambda表达式需要"函数式接口"的支持
- 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口,可以使用注解@FunctionalInterface修饰
- 可以检查是否是函数式接口
`public class TestLambda { public static void main(String[] args) { test4(); }
public static void Test1() {
Runnable runnable = () -> {
System.out.println("hello lambda");
};
runnable.run();
}
public static void Test2() {
Comsumer<String> con = x -> System.out.println(x);
con.accpet("传参");
}
public static void test3() {
Comparator<Integer> com = (x, y) -> {
System.out.println("函数式接口");
return Integer.compare(x, y);
};
System.out.println(com.compare(1, 2));
}
public static void test4() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
System.out.println(com.compare(1, 2));
}
@Test
public void test5(){
Integer operation = operation(100, (x) -> x * x);
System.out.println(operation);
}
public Integer operation(Integer num,MyFunction<Integer> mf){
return mf.getValue(num);
}
List<Integer> ins = Arrays.asList(
10,35,20,5
);
/**
* 练习
*/
@Test
public void test6(){
Collections.sort(ins,(e1,e2) -> {
return Integer.compare(e1,e2);
});
System.out.println(ins.toString());
}
@Test
public void test7(){
String trimStr = strHandler("\t\t\t 这时一个测试字段 ", (str) -> str.trim());
System.out.println(trimStr);
String upperCase = strHandler("adksdks", (str) -> str.toUpperCase());
System.out.println(upperCase);
String subStr = strHandler("dsjldsjldksldksldkl", (str) -> str.substring(2, 5));
System.out.println(subStr);
}
/**
* 用于处理字符串
*/
public String strHandler(String str,MyFunction<String> mf){
return mf.getValue(str);
}
} `
- java8 内置的四大核心函数式接口
- Comsumer:消费型接口
- void accept(T t);
- Supplier:提供型接口
- T get();
- Function:函数型接口
- R apply(T t);
- Predicate:断言型接口
- boolean test(T t);
- Supplier
- 无中生有
- ()->结果
- Function
- 一个函数一个结果
- (参数)->结果
- BiFunction
- (参数1,参数2)->结果
- Consumer
- 消费者 一个参数没结果
- (参数)->void
- BiConsumer
- (参数1,参数2)->void
- 方法引用和构造器引用
- 主要有三种语法格式:
- 对象::实例方法名
- 类::静态方法名
- 类::实例方法名
- 注意:1.Lambda体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保持一致
- 2.若Lambda参数列表中的第一参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用Class::method
- 构造器引用:
- 格式:
- ClassName::new
- 注意:需要调用的构造器的参数列表与函数式接口中抽象方法的参数列表保持一致
- 数组引用:
- 格式:
- Type[]::new
`
public class TestLambda2 {
@Test
public void test1() {
happy(10000, (m) -> {
System.out.println("消费" + m);
});
}
public void happy(double money, Consumer<Double> con) {
con.accept(money);
}
@Test
public void test2() {
List<Integer> numList = getNumList(10, () -> {
return (int) (Math.random() * 100);
});
System.out.println(numList);
}
public List<Integer> getNumList(int num, Supplier<Integer> sup) {
List<Integer> nums = new ArrayList<>();
for (int i = 0; i < num; i++) {
nums.add(sup.get());
}
return nums;
}
@Test
public void test3() {
Integer integer = doubleHandler(15.5, (dou) ->
dou.intValue()
);
System.out.println(integer);
}
public Integer doubleHandler(Double dou, Function<Double, Integer> fun) {
return fun.apply(dou);
}
@Test
public void test4() {
// System.out.println(isbigTen(10,(i)->{ // if(i>10){ // return true; // } // return false; // }));
Predicate<Integer> pre = i -> {
if (i > 10) {
return true;
}
return false;
};
System.out.println(pre.test(15));
}
public boolean isbigTen(Integer i, Predicate<Integer> pre) {
return pre.test(i);
}
@Test
public void test5() {
BiPredicate<String, String> bpd = String::equals;
System.out.println(bpd.test("tr", "tr"));
}
/**
* 构造器引用
*/
@Test
public void test6() {
Supplier<XsasLandObject> sup = () -> new XsasLandObject();
Supplier<XsasLandObject> sup2 = XsasLandObject::new;
XsasLandObject xsasLandObject = sup2.get();
}
@Test
public void test7() {
Function<Integer, String[]> fun = (x) -> new String[x];
String[] apply = fun.apply(10);
System.out.println(apply.length);
Function<Integer, String[]> fun2 = String[]::new;
String[] apply1 = fun2.apply(10);
System.out.println(apply1.length);
}
} `