Lambda表达式是Java 8引入的新特性之一,它极大地简化了代码,特别是在处理集合和使用函数式接口时。通过Lambda表达式,我们可以将函数作为参数传递,提高了代码的简洁性和可读性。本文将介绍Lambda表达式的语法、应用场景以及常见用法。
Lambda表达式是一种匿名函数,用于简化对接口的实现。在没有Lambda之前,我们通常通过匿名内部类的方式来实现接口,而使用Lambda可以用更简洁的方式表示。
(参数列表) -> { 方法体 }
语法解析:
()
:参数列表,可以为空或包含多个参数。->
:Lambda操作符,表示将参数传递给方法体。{}
:方法体,包含具体实现逻辑。若方法体只有一行语句,可以省略大括号和return
关键字。使用匿名内部类
List<String> list = Arrays.asList("Java", "Python", "C++");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
System.out.println(list);
使用Lambda表达式
List<String> list = Arrays.asList("Java", "Python", "C++");
list.sort((s1, s2) -> s1.compareTo(s2));
System.out.println(list);
✅ 使用Lambda表达式后,代码简洁了很多。
如果方法没有参数,可以使用空括号:
Runnable runnable = () -> System.out.println("无参数的Lambda表达式");
new Thread(runnable).start();
如果只有一个参数,可以省略括号:
Consumer<String> consumer = s -> System.out.println(s);
consumer.accept("单个参数的Lambda表达式");
有多个参数时,需要使用括号:
BiFunction<Integer, Integer, Integer> sum = (a, b) -> a + b;
System.out.println(sum.apply(5, 10));
{}
和return
Function<Integer, Integer> square = x -> x * x;
System.out.println(square.apply(4));
{}
和return
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> {
int result = a * b;
System.out.println("结果:" + result);
return result;
};
multiply.apply(3, 4);
函数式接口是只包含一个抽象方法的接口,可以使用@FunctionalInterface
注解进行标记。Lambda表达式可以简化对函数式接口的实现。
自定义函数式接口示例
@FunctionalInterface
public interface MyFunctionalInterface {
void show();
}
使用Lambda表达式实现:
MyFunctionalInterface myInterface = () -> System.out.println("Lambda实现函数式接口");
myInterface.show();
Java提供了许多内置的函数式接口,常用的有:
Consumer
:消费型接口,有入参无返回值。Supplier
:供给型接口,无入参有返回值。Function
:函数型接口,有入参有返回值。Predicate
:断言型接口,返回布尔值。import java.util.function.*;
public class LambdaDemo {
public static void main(String[] args) {
// Consumer示例
Consumer<String> consumer = s -> System.out.println("消费:" + s);
consumer.accept("Java");
// Supplier示例
Supplier<String> supplier = () -> "提供数据";
System.out.println(supplier.get());
// Function示例
Function<Integer, String> function = i -> "数字:" + i;
System.out.println(function.apply(10));
// Predicate示例
Predicate<Integer> predicate = n -> n > 5;
System.out.println(predicate.test(10));
}
}
List
遍历使用Lambda表达式简化遍历:
List<String> list = Arrays.asList("Java", "Python", "C++");
// 普通遍历
for (String item : list) {
System.out.println(item);
}
// 使用Lambda表达式
list.forEach(s -> System.out.println(s));
Map
遍历Map<String, Integer> map = new HashMap<>();
map.put("Java", 1);
map.put("Python", 2);
map.put("C++", 3);
// Lambda遍历Map
map.forEach((k, v) -> System.out.println(k + " -> " + v));
Stream
流操作Lambda表达式与Stream
流结合,可以实现简洁的数据处理:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// 筛选偶数并计算平方
List<Integer> squares = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println(squares);
final
或等效于final
的(即不可被修改)。int num = 10;
Runnable r = () -> System.out.println(num);
r.run();
✅ 不可以在Lambda表达式内修改num
的值。
@FunctionalInterface
interface ThrowingConsumer<T> {
void accept(T t) throws Exception;
}