1.Lambda 表达式
一、Lambda 表达式的基础语法:Java8中引入了一个新的操作符 “->” 该操作符称为箭头操作符或 Lambda 操作符, 箭头操作符将 Lambda 表达式拆分成两部分:
- 左侧:Lambda 表达式的参数列表
- 右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
二.语法
格式一:无参数,无返回值
() -> System.out.println("Hello Lambda!");
@Test
public void test1(){
int num = 0;//jdk 1.7 前,必须是 final
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello World!" + num);
}
};
r.run();
System.out.println("-------------------------------");
Runnable r1 = () -> System.out.println("Hello Lambda!");
r1.run();
}
格式二:有一个参数,并且无返回值
(x) -> System.out.println(x)
@Test
public void test2(){
Consumer<String> con = x -> System.out.println(x);
con.accept("test!");
}
格式三:若只有一个参数,小括号可以省略不写
x -> System.out.println(x)
格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
@Test
public void test3(){
Comparator<Integer> com = (x, y) -> {
System.out.println("函数式接口");
return Integer.compare(x, y);
};
}
格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
@Test
public void test4(){
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
}
格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
(Integer x, Integer y) -> Integer.compare(x, y);
三、Lambda 表达式需要“函数式接口”的支持
函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰可以检查是否是函数式接口
2.函数式接口
-
函数式接口中只有一个抽象方法,可用@FunctionalInterface修饰
Java8 内置的四大核心函数式接口Consumer<T> : 消费型接口 void accept(T t); ---------------example:----------------- @Test public void test1(){ happy(10000, (m) -> System.out.println("消费:" + m + "元")); } public void happy(double money, Consumer<Double> con){ con.accept(money); }
Supplier<T> : 供给型接口
T get();
---------------example:-----------------
@Test
public void test2(){
List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
for (Integer num : numList) {
System.out.println(num);
}
}
//需求:产生指定个数的整数,并放入集合中
public List<Integer> getNumList(int num, Supplier<Integer> sup){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer n = sup.get();
list.add(n);
}
return list;
}
Function<T, R> : 函数型接口
R apply(T t);
---------------example:-----------------
@Test
public void test3(){
String newStr = strHandler("\t\t\t 爱我中华 ", (str) -> str.trim());
System.out.println(newStr);
String subStr = strHandler("爱我中华", (str) -> str.substring(2, 5));
System.out.println(subStr);
}
//需求:用于处理字符串
public String strHandler(String str, Function<String, String> fun){
return fun.apply(str);
}
Predicate<T> : 断言型接口
boolean test(T t);
---------------example:-----------------
@Test
public void test4(){
List<String> list = Arrays.asList("Hello", "java8", "Lambda", "www", "ok");
List<String> strList = filterStr(list, (s) -> s.length() > 3);
for (String str : strList) {
System.out.println(str);
}
}
//需求:将满足条件的字符串,放入集合中
public List<String> filterStr(List<String> list, Predicate<String> pre){
List<String> strList = new ArrayList<>();
for (String str : list) {
if(pre.test(str)){
strList.add(str);
}
}
return strList;
}
3.方法引用与构造器引用
一、方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用(可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
-
对象的引用 :: 实例方法名
@Test public void test2(){ Employee emp = new Employee(101, "张三", 18, 9999.99); Supplier<String> sup = () -> emp.getName(); System.out.println(sup.get()); System.out.println("----------------------------------"); Supplier<String> sup2 = emp::getName; System.out.println(sup2.get()); } @Test public void test1(){ PrintStream ps = System.out; Consumer<String> con = (str) -> ps.println(str); con.accept("Hello World!"); System.out.println("--------------------------------"); Consumer<String> con2 = ps::println; con2.accept("Hello Java8!"); Consumer<String> con3 = System.out::println; }
-
类名 :: 静态方法名
@Test public void test4(){ Comparator<Integer> com = (x, y) -> Integer.compare(x, y); System.out.println("-------------------------------------"); Comparator<Integer> com2 = Integer::compare; } @Test public void test3(){ BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y); System.out.println(fun.apply(1.5, 22.2)); System.out.println("--------------------------------------------------"); BiFunction<Double, Double, Double> fun2 = Math::max; System.out.println(fun2.apply(1.2, 1.5));
-
类名 :: 实例方法名
@Test public void test5(){ BiPredicate<String, String> bp = (x, y) -> x.equals(y); System.out.println(bp.test("abcde", "abcde")); System.out.println("-----------------------------------------"); BiPredicate<String, String> bp2 = String::equals; System.out.println(bp2.test("abc", "abc")); System.out.println("-----------------------------------------"); Function<Employee, String> fun = (e) -> e.show(); System.out.println(fun.apply(new Employee())); System.out.println("-----------------------------------------"); Function<Employee, String> fun2 = Employee::show; System.out.println(fun2.apply(new Employee())); }
注意:
- ①方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
- ②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
二、构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
类名 :: new
@Test
public void test7(){
Function<String, Employee> fun = Employee::new;
BiFunction<String, Integer, Employee> fun2 = Employee::new;
}
@Test
public void test6(){
Supplier<Employee> sup = () -> new Employee();
System.out.println(sup.get());
System.out.println("------------------------------------");
Supplier<Employee> sup2 = Employee::new;
System.out.println(sup2.get());
}
三、数组引用
-
Type:: new
@Test public void test8(){ Function<Integer, String[]> fun = (args) -> new String[args]; String[] strs = fun.apply(10); System.out.println(strs.length); System.out.println("--------------------------"); Function<Integer, Employee[]> fun2 = Employee[] :: new; Employee[] emps = fun2.apply(20); System.out.println(emps.length); }