好处
- 简洁:lambda表达式可以使代码更加简洁,减少冗余的匿名内部类。
- 函数式编程:lambda表达式支持函数式编程,可以方便地将函数作为参数传递给其他方法。
- 并行处理:lambda表达式可以与Java 8中的Stream API结合使用,实现并行处理,提高程序性能。
- 延迟执行:lambda表达式可以实现延迟执行,只有在需要时才会执行相应的操作。
- 易于阅读:lambda表达式的语法更加清晰,易于阅读和理解。
举例子对比
假设我们有一个需求,需要对一个整数列表进行排序,可以使用传统的匿名内部类实现
List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 6);
numbers.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) { return o1 - o2; }
});
使用Lambda表达式后,代码变得更加简洁:
List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 6);
numbers.sort((o1, o2) -> o1 - o2);
另外,Lambda表达式还可以用于实现Runnable接口,例如:
Runnable runnable = () -> System.out.println("Hello, World!"); runnable.run();
从上面可以看出使用lambda表达式之后对于可读性和代码简洁度方面都有提升。
十种用法
那么以下举一些用法:
1. 使用Lambda表达式进行集合遍历
List<String> list = Arrays.asList("apple", "banana", "orange");
for (String fruit : list) {
System.out.println(fruit);
}
List<String> list2 = Arrays.asList("apple", "banana", "orange");
list2.forEach(fruit -> System.out.println(fruit));
2. 使用Lambda表达式进行排序
List<String> list2 = Arrays.asList("apple", "banana", "orange");
Collections.sort(list2, new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
List<String> list = Arrays.asList("apple", "banana", "orange");
Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
3. 使用Lambda表达式进行过滤
List<String> list = Arrays.asList("apple", "banana", "orange");
List<String> filteredList = new ArrayList<String>();
for (String fruit : list) {
if (fruit.startsWith("a")) {
filteredList.add(fruit);
}
}
List<String> list2 = Arrays.asList("apple", "banana", "orange");
List<String> filteredLists = list2.
stream().
filter(fruit -> fruit.startsWith("a")).
collect(Collectors.toList());
4. 使用Lambda表达式进行映射
List<String> list = Arrays.asList("apple", "banana", "orange");
List<Integer> lengths = new ArrayList<Integer>();
for (String fruit : list) {
lengths.add(fruit.length());
}
List<String> lists = Arrays.asList("apple", "banana", "orange");
List<Integer> lengthss = lists.stream().map(fruit -> fruit.length())
.collect(Collectors.toList());
5. 使用Lambda表达式进行归约
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int sum = 0;
for (int i : list) {
sum += i;
}
List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5);
int sum2 = list2.stream().reduce(0, (a, b) -> a + b);
6. 使用Lambda表达式进行分组
List<String> list = Arrays.asList("apple", "banana", "orange");
Map<Integer, List<String>> grouped = new HashMap<Integer, List<String>>();
for (String fruit : list) {
int length = fruit.length();
if (!grouped.containsKey(length)) {
grouped.put(length, new ArrayList<String>());
}
grouped.get(length).add(fruit);
}
List<String> lists = Arrays.asList("apple", "banana", "orange");
Map<Integer, List<String>> groupeds = lists.stream().collect(Collectors.groupingBy(fruit -> fruit.length()));
7. 使用Lambda表达式进行函数式接口的实现
MyInterface myObject = new MyInterface() {
public void doSomething(String input) {
System.out.println(input);
}
};
myObject.doSomething("Hello World");
MyInterface myObject = input -> System.out.println(input);
myObject.doSomething("Hello World");
8. 使用Lambda表达式进行线程的创建
Thread thread = new Thread(new Runnable() {
public void run() {
System.out.println("Thread is running.");
}
});
thread.start();
Thread threads = new Thread(() -> System.out.println("Thread is running."));
threads.start();
9. 使用Lambda表达式进行Optional的操作
String str = "Hello World";
if (str != null) {
System.out.println(str.toUpperCase());
}
Optional<String> strs = Optional.ofNullable("Hello World");
strs.map(String::toUpperCase).ifPresent(System.out::println);
10. 使用Lambda表达式进行Stream的流水线操作
List<String> list = Arrays.asList("apple", "banana", "orange");
List<String> filteredList = new ArrayList<String>();
for (String fruit : list) {
if (fruit.startsWith("a")) {
filteredList.add(fruit.toUpperCase());
}
}
Collections.sort(filteredList);
List<String> lists = Arrays.asList("apple", "banana", "orange");
List<String> filteredLists = lists.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());