❌ 未使用Lambda表达式:
List list = Arrays.asList("apple", "banana", "orange");
for (String fruit : list) {
System.out.println(fruit);
}
✅ 使用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, new Comparator() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
✅ 使用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 = new ArrayList();
for (String fruit : list) {
if (fruit.startsWith("a")) {
filteredList.add(fruit);
}
}
✅ 使用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 = new ArrayList();
for (String fruit : list) {
lengths.add(fruit.length());
}
✅ 使用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 = 0;
for (int i : list) {
sum += i;
}
✅ 使用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 grouped = new HashMap();
for (String fruit : list) {
int length = fruit.length();
if (!grouped.containsKey(length)) {
grouped.put(length, new ArrayList());
}
grouped.get(length).add(fruit);
}
✅ 使用Lambda表达式:
List list = Arrays.asList("apple", "banana", "orange");
Map grouped = list.stream()
.collect(Collectors.groupingBy(fruit -> fruit.length()));
❌ 未使用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");
❌ 未使用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();
❌ 未使用Lambda表达式:
String str = "Hello World";
if (str != null) {
System.out.println(str.toUpperCase());
}
✅ 使用Lambda表达式:
Optional str = Optional.ofNullable("Hello World");
str.map(String::toUpperCase)
.ifPresent(System.out::println);
❌ 未使用Lambda表达式:
List list = Arrays.asList("apple", "banana", "orange");
List filteredList = new ArrayList();
for (String fruit : list) {
if (fruit.startsWith("a")) {
filteredList.add(fruit.toUpperCase());
}
}
Collections.sort(filteredList);
✅ 使用Lambda表达式:
List list = Arrays.asList("apple", "banana", "orange");
List filteredList = list.stream().filter(fruit -> fruit.startsWith("a"))
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());