假设我们有一个需求,需要对一个整数列表进行排序,可以使用传统的匿名内部类实现
List numbers = Arrays.asList(5, 3, 8, 1, 6);
numbers.sort(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) { return o1 - o2; }
});
使用Lambda表达式后,代码变得更加简洁:
List 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 list = Arrays.asList("apple", "banana", "orange");
for (String fruit : list) {
System.out.println(fruit);
}
List list2 = Arrays.asList("apple", "banana", "orange");
list2.forEach(fruit -> System.out.println(fruit));
List list2 = Arrays.asList("apple", "banana", "orange");
Collections.sort(list2, new Comparator() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
List list = Arrays.asList("apple", "banana", "orange");
Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
List list = Arrays.asList("apple", "banana", "orange");
List filteredList = new ArrayList();
for (String fruit : list) {
if (fruit.startsWith("a")) {
filteredList.add(fruit);
}
}
List list2 = Arrays.asList("apple", "banana", "orange");
List filteredLists = list2.
stream().
filter(fruit -> fruit.startsWith("a")).
collect(Collectors.toList());
List list = Arrays.asList("apple", "banana", "orange");
List lengths = new ArrayList();
for (String fruit : list) {
lengths.add(fruit.length());
}
List lists = Arrays.asList("apple", "banana", "orange");
List lengthss = lists.stream().map(fruit -> fruit.length())
.collect(Collectors.toList());
List list = Arrays.asList(1, 2, 3, 4, 5);
int sum = 0;
for (int i : list) {
sum += i;
}
List list2 = Arrays.asList(1, 2, 3, 4, 5);
int sum2 = list2.stream().reduce(0, (a, b) -> a + b);
6. 使用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);
}
List lists = Arrays.asList("apple", "banana", "orange");
Map> 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 strs = Optional.ofNullable("Hello World");
strs.map(String::toUpperCase).ifPresent(System.out::println);
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);
List lists = Arrays.asList("apple", "banana", "orange");
List filteredLists = lists.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());