Lambda表达式
Lambda简介
Lambda 表达式(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(
Lambda语法
//Lambda 表达式的基础语法 :Java8 中引入的新操作符 ->
//操作符左侧: Lambda 表达式的参数列表
//操作符右侧: Lambda 表达式中所需要执行的功能 Lambda体
语法格式一
//语法格式一 : 无参无返回值
// () -> 功能体
@Test
public void test1() {
//匿名内部类
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("今天周五");
}
};
r.run();
System.out.println("----------------------------------");
//Lambda表达式
Runnable r1 = () -> System.out.println("明天就周六了");
r1.run();
}
语法格式二
//语法格式二 : 有一个参数无返回值
// (参数) -> 功能体(参数)
// 只有一个参数时,小括号可以省略
@Test
public void test2() {
Consumer consumer = (x) -> System.out.println(x);
consumer.accept("摇啊摇");
语法格式三
//语法格式三 : 多个参数有返回值,多条功能语句
// (参数,参数) -> { 功能体 返回值}
//Lambda 只有一个语句 大括号 return 可以省略
//类型推断:在执行javac编译程序时,JVM根据程序的上下文推断出了参数的类型。
@Test
public void test3() {
Comparator<Integer> comparator = (x, y) -> {
System.out.println("Lambda");
return Integer.compare(x,y);
};
System.out.println( comparator.compare(1,2));
//省略情况
//Comparator<Integer> com=(Integer x, Integer y)-> Integer.compare(x,y);
Comparator<Integer> com=(x, y)-> Integer.compare(x,y);//省略了数据类型
System.out.println(com.compare(4,2));
}
Java8 四大内置核心函数接口
Comsumer消费形接口
// Comsumer<T>消费形接口
// void accept(T t); 将传入参数做相应的逻辑处理,无返回值。
@Test
public void test1() {
happy(100, m -> System.out.println("看电影消费了:" + m + "元"));
}
public void happy(int moeny, Consumer<Integer> consumer) {
consumer.accept(moeny);
}
Supplier 供给型接口
//Supplier<T>
//T get(); 做相应的逻辑处理,返回一个对象,可以用来生成 集合 ,数组 等等
@Test
public void test2() {
List<Integer> list = getNumList(5, () -> (int) (Math.random() * 100));
list.stream().forEach(System.out::println);
}
//产生指定个数整数,放入集合
public List<Integer> getNumList(int num, Supplier<Integer> sup) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < num; i++) {
list.add(sup.get());
}
return list;
}
Function<T, R> 函数型接口
//Function<T, R> 函数型接口
//R apply(T t); 根据传入的参数,做相应的逻辑处理,返回一个处理结果
@Test
public void test3() {
String string = "wangqi";
String newStr = strHandler(string, (str) -> str.toUpperCase());
System.out.println(newStr);
String newStr1 = strHandler(string, (str) -> str.substring(4,5).toUpperCase());
System.out.println(newStr1);
}
public String strHandler(String str, Function<String, String> function) {
return function.apply(str);
}
redicate 断言型接口
//Predicate<T> 断言型接口 boolean test(T t);
// test(T t); 根据传入的参数,做相应的逻辑处理,返回一个布尔值,可以用在集合的过滤等操作。
@Test
public void test4() {
List<String> list = Arrays.asList("Hello", "World", "java", "Python", "Vue");
filterInt(list, (str) -> str.length() < 4).stream().forEach(System.out::println);
}
public List<String> filterInt(List<String> list, Predicate<String> pre) {
List<String> l = new ArrayList<>();
for (String string : list) {
if (pre.test(string))
l.add(string);
}
return l;
}
方法引用
1.对象::实例方法名
//对象::实例方法名称
@Test
public void test1() {
Consumer<String> con = (x) -> System.out.println(x);
PrintStream ps = System.out;
Consumer<String> con1 = ps::println;
}
2.类::静态方法名
//类::静态方法名
@Test
public void test2(){
Comparator<Integer> com=(x,y)->Integer.compare(x,y);
Comparator<Integer> com1=Integer::compare;
// compare(x,y) x>y 返回 1 ,x<y 返回-1 x==y 返回0
System.out.println(com.compare(1,0));//结果:1
System.out.println(com1.compare(1,0));//结果:1
}
3.类::实例方法名
//类::实例方法名
@Test
public void test3(){
BiPredicate<String,String> bp=(x,y)->x.equals(y);
BiPredicate<String,String> bp2=String::equals;
System.out.println(bp.test("1","2"));
System.out.println(bp.test("1","2"));
}
构造器引用
@Test
public void test2() {
//引用无参构造器
Supplier<Employee> supplier = Employee::new;
System.out.println(supplier.get());
//引用有参构造器
Function<String, Employee> function = Employee::new;
System.out.println(function.apply("喜羊羊"));
BiFunction<String, Integer, Employee> employeeBiFunction = Employee::new;
System.out.println(employeeBiFunction.apply("懒羊羊", 18));