假设我们有一个需求,需要对一个整数列表进行排序,可以使用传统的匿名内部类实现
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表达式之后对于可读性和代码简洁度方面都有提升。
那么以下举一些用法:
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));
?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));
?
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());
?
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());
?
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()));
?
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");
?
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();
?
?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);
?
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());
?