1.Lambda表达式
经过尚硅谷老师的视频讲解 总结下来的笔记
package com.zhangan.lambda;
/**
* @Author: 张安
* @Date: 2021/5/24 22:16
* @Description: 学习Lambda语法
*/
import com.zhangan.entity.Employee;
import com.zhangan.service.MyFun;
import com.zhangan.service.MyFunction;
import com.zhangan.service.MyFunction2;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 左侧:Lambda表达式的参数列表
* 右侧:Lambda表达式中所需执行的功能,即 Lambda 体*
* <p>
* 语法格式一:无参数,无返回值*
* () -> System.out.print1n( "Hello Lambda! ");
* <p>
* 语法格式二:有一个参数,并且无返回值
* (x) -> System.out.println(x)
* <p>
* 语法格式三:若只有一个参数,小括号可以省略不写
* x -> system.out.print1n(x)
* <p>
* 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
* Comparator<Integer>com = (×, y) ->{
* System.out.println("函数式接口");
* return Integer.compare(x, y);}
* <p>
* 语法格式五:若Lambda 体中只有一条语句,return和大括号都可以省略不写
* Comparator<Integer> com = (x, y) ->Integer.compare(x,y);
* <p>
* 语法格式六: Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即类型推断
* (Integer x,Integer y) -> Integer.compare(x, y);
* <p>
* <p>
* <p>
* 需要函数式接口的支持
* 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。可以使用注解@FunctionalInterface修饰
* 可以检查是否是函数式接口
*/
public class Lambda2 {
public static void main(String[] args) {
}
@Test
public void test1() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
}
public Integer operation(Integer num, MyFun mf) {
return mf.getValue(num);
}
//对于个数运算
@Test
public void test2() {
Integer integer = operation(100, (x) -> x * x);
System.out.println(integer);
Integer integer1 = operation(200, (x) -> x + 200);
System.out.println(integer1);
}
List<Employee> employees = Arrays.asList(
new Employee("张安", 18, 99999),
new Employee("张三", 35, 88888),
new Employee("李四", 45, 77777),
new Employee("王五", 20, 66666),
new Employee("赵六", 36, 44444)
);
//
@Test
public void tset3() {
Collections.sort(employees, (e1, e2) -> {
if (e1.getAge() == e2.getAge()) {
return e1.getName().compareTo(e2.getName());
} else {
return Integer.compare(e1.getAge(), e2.getAge());
}
});
for (Employee employee : employees) {
System.out.println(employee);
}
}
//用于处理字符串
public String strHandler(String str, MyFunction mf) {
return mf.getValue(str);
}
@Test
public void name4() {
String s = strHandler("Im handsome", (str) -> str.toUpperCase());
System.out.println(s);
}
//对于两个long型数据处理
public void operationForLong(Long l1, Long l2, MyFunction2<Long, Long> mf) {
System.out.println(mf.getValue(l1, l2));
}
@Test
public void name5() {
operationForLong(100L, 200L, (x, y) -> x + y);
}
}
2.函数式接口
package com.zhangan.lambda;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* @Author: 张安
* @Date: 2021/5/24 22:57
* @Description: java8内置的4大核心函数式接口
* <p>
* <p>
* Consumer<T>:消费型接口 void accept(T t);
* Supplier<T>:供给型接口 T get();
* Function<T,R>函数型接口 R apply(T t);
* Predicate<T>断言型接口 boolean test(T t);
*/
public class Lambda3 {
//Consumer<T>:消费型接口 void accept(T t);
public void happy(double money, Consumer<Double> con) {
con.accept(money);
}
@Test
public void test1() {
happy(10000, (x) -> System.out.println("花费" + x + "元"));
}
//Supplier<T>:供给型接口 T get();
//产生几个 (什么样的) 整数放到集合
public List<Integer> getNumList(int num, Supplier<Integer> sp) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer integer = sp.get();
list.add(integer);
}
return list;
}
@Test
public void name2() {
List<Integer> list = getNumList(5, () -> (int) (Math.random() * 100));
list.forEach(System.out::println);
}
//Function<T,R>函数型接口 R apply(T t);
//设计一个处理字符串的
public String operationString(String str, Function<String, String> fun) {
return fun.apply(str);
}
@Test
public void name3() {
String string = operationString("我是大帅哥张安", (x) -> x.substring(1, 2));
System.out.println(string);
}
//Predicate<T>断言型接口 boolean test(T t);
//需求 将满足条件的字符串 放入集合
public List<String> filterString(List<String> list, Predicate<String> pd) {
List<String> res = new ArrayList<>();
for (String s : list) {
if (pd.test(s)) {
res.add(s);
}
}
return res;
}
@Test
public void name4() {
List<String> stringList = Arrays.asList("aa", "bbb", "cccc", "dddd");
List<String> stringList1 = filterString(stringList, (x) -> x.length() > 3);
for (String s : stringList1) {
System.out.println(s);
}
}
}
3.方法引用与构造器引用
package com.zhangan.lambda;
import com.zhangan.entity.Employee;
import org.junit.Test;
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @Author: 张安
* @Date: 2021/5/25 08:49
* @Description: 构造器引用
* <p>
* <p>
* 构造器引用
* <p>
* 格式
* ClassName::new
* 注意:需要调用的构造器的参数列表要与函数式接口中抽象方法的参数列表保持一致!
* <p>
* <p>
* 数组引用:
* Type: :new;
*/
public class TestConstructRef {
@Test
public void name1() {
Supplier<Employee> sup = () -> new Employee();
//参数列表一致 调用的是无参构造
Supplier<Employee> sup2 = Employee::new;
Employee employee = sup2.get();
System.out.println(employee);
}
@Test
public void name2() {
Function<Integer, Employee> fun = (x) -> new Employee(x);
Function<Integer, Employee> fun2 = Employee::new;
Employee apply = fun2.apply(12);
System.out.println(apply);
}
@Test
public void name3() {
Function<Integer, String[]> fun = (x) -> new String[x];
Function<Integer, String[]> fun2 = String[]::new;
String[] apply = fun2.apply(10);
System.out.println(Arrays.toString(apply));
}
}
package com.zhangan.lambda;
import com.zhangan.entity.Employee;
import org.junit.Test;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @Author: 张安
* @Date: 2021/5/24 23:40
* @Description: 方法引用
* <p>
* 方法引用:若 Lambda 体中的内容有方法已经实现了,我们可以使用"方法引用"
* (可以理解为方法引用是Lambda表达式的另外一种表现形式)
* <p>
* 主要有三种语法格式:
* 对象::实例方法名
* 类::静态方法名
* 类::实例方法名
* <p>
* 注意:
* Lambda 体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保持一致!
* <p>
* 若Lambda 参数列表中的第一参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName :: method
*/
public class TestMethodRef {
//对象::实例方法名
@Test
public void test1() {
Consumer<String> con = (x) -> System.out.println(x);
Consumer<String> con2 = System.out::println;
con2.accept("aaaa");
}
//对象::实例方法名
@Test
public void name2() {
Employee employee = new Employee("张安", 18, 100000);
Supplier<String> sup = () -> employee.getName();
String s = sup.get();
System.out.println(s);
Supplier<String> sup2 = employee::getName;
String s2 = sup2.get();
System.out.println(s2);
}
//类::静态方法名
@Test
public void name3() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
Comparator<Integer> com2 = Integer::compare;
int compare = com2.compare(1, 2);
System.out.println(compare);
}
//类::实例方法名
@Test
public void name4() {
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
BiPredicate<String, String> bp2 = String::equals;
boolean test = bp2.test("zhangan", "anan");
System.out.println(test);
}
}