JAVA8 - Lambda
预览
package com.day01;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Demo1 {
List<Employee> list = Arrays.asList(
new Employee("张三", 66, 5555.555),
new Employee("李四", 44, 4444.444),
new Employee("王五", 11, 3333.333),
new Employee("赵七", 33, 8888.888),
new Employee("老八", 22, 9999.999)
);
@Test
public void test01(){
//查询年纪大于33岁的员工
list.stream()
.filter((e) -> e.getAge() > 33)
.forEach(System.out::println);
System.out.println("---------------------------------------");
//查询年纪大于33岁的员工, 只取第一条结果
list.stream()
.filter((e) -> e.getAge() > 33)
.limit(1)
.forEach(System.out::println);
System.out.println("---------------------------------------");
//获得list中所有员工的姓名
list.stream()
.map((e) -> e.getName())
.forEach(System.out::println);
}
}
// out:
Employee{name='张三', age=66, salary=5555.555}
Employee{name='李四', age=44, salary=4444.444}
---------------------------------------
Employee{name='张三', age=66, salary=5555.555}
---------------------------------------
张三
李四
王五
赵七
老八
Lambda 语法格式
package com.day01;
import org.junit.Test;
import java.util.Comparator;
import java.util.function.Consumer;
/**
*
* JAVA8 中引入了一个新的操作符"->" 该操作符称为箭头操作符或Lambda操作符,箭头操作符将Lambda拆分成两部分:
* 左侧:Lambda 表达式中的参数列表
* 右侧:Lambda 表达式中所需执行的功能,即Lambda体
*
* Lambda 表达式需要"函数式接口"的支持
*
* 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。可以使用注解 @FunctionalInterface 修饰
* 可以检查是否是函数式接口
*
* 注意:
* 1. Lambda 体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的参数列表和返回值类型保持一致
*
* */
public class Demo2 {
@Test
public void test01() {
/**
* 语法格式一:无参数,无返回值
* () -> System.out.println("Hello Lambda !")
*/
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello Lambda !");
}
};
r.run();
System.out.println("--------------------------------------");
Runnable r1 = () -> System.out.println("Hello Lambda !");
r1.run();
}
@Test
public void test02() {
/**
* 语法格式二: 有一个参数,无返回值
* (x) -> System.out.println("x!") or x -> System.out.println("x!")
*/
Consumer c = new Consumer() {
@Override
public void accept(Object o) {
System.out.println(o);
}
};
c.accept("xx");
System.out.println("-----------------------------");
Consumer c1 = x -> System.out.println(x); /
c1.accept("xx");
}
@Test
public void test03() {
/**
* 语法格式三: 有两个以上参数,有返回值,并且Lambda 中有多条语句
* Comparator<Integer> c1 = (x, y) -> {
* System.out.println("xxxxxxxxxxx");
* return Integer.compare(x, y);
* };
*/
Comparator<Integer> c = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, 02);
}
};
System.out.println("------------------------------");
Comparator<Integer> c1 = (x, y) -> {
System.out.println("xxxxxxxxxxx");
return Integer.compare(x, y);
};
}
@Test
public void test04() {
/**
* 语法格式四: 若Lambda体中只有一条语句,则return 和 大括号都可以省略
* Comparator<Integer> c1 = (x, y) -> Integer.compare(x, y);
*/
Comparator<Integer> c = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, 02);
}
};
System.out.println("------------------------------");
Comparator<Integer> c1 = (x, y) -> Integer.compare(x, y);
}
@Test
public void test05() {
/**
* 语法格式五:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编辑器通过上下文推断出数据类型,即 "类型推断"
* Comparator<Integer> c1 = (Integer x, Integer y) -> Integer.compare(x, y);
*/
Comparator<Integer> c = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, 02);
}
};
System.out.println("------------------------------");
Comparator<Integer> c1 = (Integer x, Integer y) -> Integer.compare(x, y);
}
}
使用Lambda
// MyFunc.java 定义函数式接口:
package com.day01;
@FunctionalInterface
public interface MyFunc {
Integer calc(Integer i);
}
// Demo3.java :测试函数式接口的使用
package com.day01;
import org.junit.Test;
public class Demo3 {
@Test
public void test01(){
Integer i = getValue(10, x -> 10/2);
System.out.println(i); # out:5
}
public Integer getValue(Integer i, MyFunc mf){
return mf.calc(i);
}
}
JAVA 8 内置的四大核心函数式接口:
* Consumer<T>: 消费型接口
void accept(T t);
* Supplier<T>: 供给型接口
T get();
* Function<T, R>: 函数型接口
R apply(T t);
* Predicate<T>: 断言型接口
boolean test(T t);
方法引用
package com.day01;
import org.junit.Test;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* 方法引用:若Lambda 体中的内容有方法且已经实现了,我们可以使用"方法引用"(可以理解为方法引用是 Lambda 表达式的另外一种表现形式)
* 主要有三种语法格式:
* 1. 对象::实例方法名
* 2. 类::静态方法名
* 3. 类::实例方法名
*
* 注意:
* 1. 若Lambda 参数列表中的第一个参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用 Class::method
*/
public class MethodRefDemo {
//1. 对象::实例方法名
@Test
public void test01(){
PrintStream out = System.out;
out.println("旧有调用方式");
Consumer<String> c = System.out::println;
c.accept("Lambda 表现方式");
}
//2.类::静态方法名
@Test
public void test02(){
// 比较两个integer的大小
Comparator<Integer> c = (x ,y) -> Integer.compare(x,y);
Comparator<Integer> c2 = Integer::compare;
int compare = c2.compare(1, 2);
System.out.println(compare);
}
//3. 类::实例方法名
@Test
public void test03(){
BiPredicate<String, String> bp1 = (x,y) -> x.equals(y);
System.out.println(bp1.test("a","b"));
BiPredicate<String, String> bp2 = String::equals;
System.out.println(bp2.test("a","a"));
}
}
构造器引用
package com.day01;
import org.junit.Test;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 构造器引用:
*
* 格式: ClassName::new
*
* 注意:需要调用的构造器的参数列表要与函数式接口中抽象方法的参数列表保持一致!
*/
public class ConstructRefDemo {
@Test
public void test01(){
//1. 调用无参的构造器
Supplier<Employee> s = Employee::new;
System.out.println(s.get().getAge()); //out: 0
//2. 调用一个参数的构造器
Function<Integer,Employee> f = Employee::new;
System.out.println(f.apply(10).getAge()); //out:10
//3. 调用两个参数的构造器
BiFunction<String, Integer, Employee> bf = Employee::new;
System.out.println(bf.apply("张三",18).getAge()); //out:18
}
}
数组引用
package com.day01;
import org.junit.Test;
import java.util.function.Function;
/**
*
* 数组引用:Type::new
*/
public class ArrayRefDemo {
@Test
public void test01(){
// 数组引用
//非Lambda 方式:
Function<Integer,String[]> f1 = (x) -> new String[x];
//数组引用方式:
Function<Integer, String[]> f2 = String[]::new;
System.out.println(f2.apply(10).length); //out: 10
}
}
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/16685536.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!