前言
Lambda 表达式是一种在古代编程语言中越来越常见的个性,能够简化代码、进步生产力。这篇文章将介绍 10 个必须把握的 Lambda 表达式,这些表达式涵盖了在理论编程中常常用到的常见场景,例如列表操作、函数组合、条件筛选等。通过学习这些 Lambda 表达式,你将更加深刻地了解函数式编程的思维,可能更加高效地编写代码。
1. 应用 Lambda 表达式进行汇合遍历
未应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
for (String fruit : list) {System.out.println(fruit);
}
应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
list.forEach(fruit -> System.out.println(fruit));
2. 应用 Lambda 表达式进行排序
未应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
Collections.sort(list, new Comparator<String>() {public int compare(String s1, String s2) {return s1.compareTo(s2);
}
});
应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
3. 应用 Lambda 表达式进行过滤
未应用 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);
}
}
应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
List<String> filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).collect(Collectors.toList());
4. 应用 Lambda 表达式进行映射
未应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
List<Integer> lengths = new ArrayList<Integer>();
for (String fruit : list) {lengths.add(fruit.length());
}
应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
List<Integer> lengths = list.stream().map(fruit -> fruit.length())
.collect(Collectors.toList());
5. 应用 Lambda 表达式进行归约
未应用 Lambda 表达式:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int sum = 0;
for (int i : list) {sum += i;}
应用 Lambda 表达式:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int sum = list.stream().reduce(0, (a, b) -> a + b);
6. 应用 Lambda 表达式进行分组
未应用 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);
}
应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
Map<Integer, List<String>> grouped = list.stream().collect(Collectors.groupingBy(fruit -> fruit.length()));
7. 应用 Lambda 表达式进行函数式接口的实现
未应用 Lambda 表达式:
public interface MyInterface {public void doSomething(String input);
}
MyInterface myObject = new MyInterface() {public void doSomething(String input) {System.out.println(input);
}
};
myObject.doSomething("Hello World");
应用 Lambda 表达式:
MyInterface myObject = input -> System.out.println(input);
myObject.doSomething("Hello World");
8. 应用 Lambda 表达式进行线程的创立
未应用 Lambda 表达式:
Thread thread = new Thread(new Runnable() {public void run() {System.out.println("Thread is running.");
}
});
thread.start();
应用 Lambda 表达式:
Thread thread = new Thread(() -> System.out.println("Thread is running."));
thread.start();
9. 应用 Lambda 表达式进行 Optional 的操作
未应用 Lambda 表达式:
String str = "Hello World";
if (str != null) {System.out.println(str.toUpperCase());
}
应用 Lambda 表达式:
Optional<String> str = Optional.ofNullable("Hello World");
str.map(String::toUpperCase).ifPresent(System.out::println);
10. 应用 Lambda 表达式进行 Stream 的流水线操作
未应用 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.toUpperCase());
}
}
Collections.sort(filteredList);
应用 Lambda 表达式:
List<String> list = Arrays.asList("apple", "banana", "orange");
List<String> filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
结尾
如果感觉对你有帮忙,能够多多评论,多多点赞哦,也能够到我的主页看看,说不定有你喜爱的文章,也能够顺手点个关注哦,谢谢。
我是不一样的科技宅,每天提高一点点,体验不一样的生存。咱们下期见!