不使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
for (String name : list) {
System.out.println(name);
}
使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
list.forEach(name -> System.out.println(name));
升序
不使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
Collections.sort(list, new Comparator() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
不使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
List filteredList = new ArrayList();
for (String name : list) {
if (name.startsWith("a")) {
filteredList.add(name);
}
}
使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
List filteredList = list.stream().filter(name -> name.startsWith("a")).collect(Collectors.toList());
映射即按照当前list顺序,根据新的规则新建一个list然后存储对应的对象
不使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
List lengths = new ArrayList();
for (String name : list) {
lengths.add(name.length());
}
使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
List lengths = list.stream().map(name -> name.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);
注意: reduce(T identity, BinaryOperator
把字符串长度相同的放在同一个链表里
不使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
Map> grouped = new HashMap>();
for (String name : list) {
int length = fruit.length();
if (!grouped.containsKey(length)) {
grouped.put(length, new ArrayList());
}
grouped.get(length).add(name);
}
使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
Map> grouped = list.stream().collect(Collectors.groupingBy(name -> name.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");
Optional类是用来解决空指针异常的。
Optional.ofNullable(T t)
:t可以为nullOptional.map(Function super T,? extends U> mapper)
:如果有值,则对其执行调用映射函数得到返回值。如果返回值不为 null,则创建包含映射返回值的Optional作为map方法返回值,否则返回空Optional。不使用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("kk", "oneone", "11");
List filteredList = new ArrayList();
for (String name : list) {
if (name.startsWith("a")) {
filteredList.add(name.toUpperCase());
}
}
Collections.sort(filteredList);
使用lambda:
List list = Arrays.asList("kk", "oneone", "11");
List filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
不使用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();