使用Lambda表达式进行集合遍历
List list = Arrays.asList("apple", "banana", "orange");
list.forEach(fruit -> System.out.println(fruit));
使用Lambda表达式进行排序
List list = Arrays.asList("apple", "banana", "orange");
Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
使用Lambda表达式进行过滤
List list = Arrays.asList("apple", "banana", "orange");
List filteredList = list.stream().filter(fruit -> fruit.startsWith("a"))
.collect(Collectors.toList());
使用Lambda表达式进行映射
List list = Arrays.asList("apple", "banana", "orange");
List lengths = list.stream()
.map(fruit -> fruit.length())
.collect(Collectors.toList());
使用Lambda表达式进行归约
List list = Arrays.asList(1, 2, 3, 4, 5);
int sum = list.stream().reduce(0, (a, b) -> a + b);
使用Lambda表达式进行分组
List list = Arrays.asList("apple", "banana", "orange");
Map<Integer, List> grouped = list.stream()
.collect(Collectors.groupingBy(fruit -> fruit.length()));
使用Lambda表达式进行函数式接口的实现
MyInterface myObject = input -> System.out.println(input);
myObject.doSomething("Hello World");
使用Lambda表达式进行线程的创建
Thread thread = new Thread(() -> System.out.println("Thread is running."));
thread.start();
使用Lambda表达式进行Optional的操作
Optional str = Optional.ofNullable("Hello World");
str.map(String::toUpperCase)
.ifPresent(System.out::println);
使用Lambda表达式进行Stream的流水线操作
List list = Arrays.asList("apple", "banana", "orange");
List filteredList = list.stream().filter(fruit -> fruit.startsWith("a"))
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());