Lambda 表达式是 Java 8 引入的一种语法特性,它允许你以更紧凑的方式表示匿名函数或代码块。Lambda 表达式主要用于函数式编程和简化代码,它在多种场景下都能发挥作用。以下是 Java 中 Lambda 表达式的主要用法总结:
这些只是 Lambda 表达式的一些常见用法。通过使用 Lambda 表达式,你可以编写更简洁、更具可读性的代码,并且在函数式编程范式中更自然地表达逻辑。请注意,在使用 Lambda 表达式时需要了解参数列表、箭头操作符和代码块的语法结构。
interface MyInterface {
void doSomething();
}
MyInterface myObj = new MyInterface() {
public void doSomething() {
System.out.println("Doing something.");
}
};
MyInterface myObj = () -> System.out.println("Doing something.");
Runnable runnable = new Runnable() {
public void run() {
System.out.println("Running...");
}
};
Runnable runnable = () -> System.out.println("Running...");
public void process(Action action) {
action.execute();
}
Action action = new Action() {
public void execute() {
System.out.println("Action executed.");
}
};
process(action);
public void process(Action action) {
action.execute();
}
process(() -> System.out.println("Action executed."));
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
for (Integer number : numbers) {
System.out.println(number);
}
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(number -> System.out.println(number));
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
Collections.sort(names, new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
区别:Lambda 表达式使排序和比较逻辑更紧凑、更可读。
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(new Runnable() {
public void run() {
System.out.println("Task executed.");
}
});
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> System.out.println("Task executed."));
区别:Lambda 表达式使多线程代码更加简洁,同时支持并行和异步任务。
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked.");
}
});
button.addActionListener(e -> System.out.println("Button clicked."));
区别:Lambda 表达式使事件处理逻辑更直观,减少了样板代码。
Validator validator = new Validator() {
public boolean isValid(String input) {
return input != null && !input.isEmpty();
}
};
Validator validator = input -> input != null && !input.isEmpty();
区别:Lambda 表达式简化了策略模式的实现,使代码更加简洁。
if (condition) {
executeDelayedTask(new Runnable() {
public void run() {
System.out.println("Delayed task executed.");
}
});
}
if (condition) {
executeDelayedTask(() -> System.out.println("Delayed task executed."));
}
区别:Lambda 表达式使延迟执行代码更紧凑。
performOperation(new Callback() {
public void onSuccess() {
System.out.println("Operation succeeded.");
}
public void onFailure() {
System.out.println("Operation failed.");
}
});
performOperation(() -> System.out.println("Operation succeeded."),
() -> System.out.println("Operation failed."));
区别:Lambda 表达式使回调函数更简洁,减少了冗余代码。
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = 0;
for (int number : numbers) {
sum += number;
}
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (x, y) -> x + y);
区别:Lambda 表达式与流式编程结合,使数据处理更加简洁、函数式。
总的来说,Lambda 表达式能够显著减少冗长的代码,提高代码的可读性和可维护性,同时支持函数式编程风格,使代码更加优雅和灵活。