1.集合遍历
不使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); for (String name : list) { System.out.println(name); }
使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); list.forEach(name -> System.out.println(name));
2.排序
升序
不使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); Collections.sort(list, new Comparator<String>() { public int compare(String s1, String s2) { return s1.compareTo(s2); } });
使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
3.过滤
不使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); List<String> filteredList = new ArrayList<String>(); for (String name : list) { if (name.startsWith("a")) { filteredList.add(name); } }
使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); List<String> filteredList = list.stream().filter(name -> name.startsWith("a")).collect(Collectors.toList());
4.映射
映射即按照当前list顺序,根据新的规则新建一个list然后存储对应的对象
不使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); List<Integer> lengths = new ArrayList<Integer>(); for (String name : list) { lengths.add(name.length()); }
使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); List<Integer> lengths = list.stream().map(name -> name.length()) .collect(Collectors.toList());
5.规约(求和)
不使用lambda:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int sum = 0; for (int i : list) { sum += i; }
使用lambda:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int sum = list.stream().reduce(0, (a, b) -> a + b);
注意: reduce(T identity, BinaryOperator<T> accumulator)方法第一个参数传入的是这个求和方法的起始值,如reduce(0, (a, b) -> a + b)输出15,reduce(3, (a, b) -> a + b)输出18
6.分组
把字符串长度相同的放在同一个链表里
不使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); Map<Integer, List<String>> grouped = new HashMap<Integer, List<String>>(); for (String name : list) { int length = fruit.length(); if (!grouped.containsKey(length)) { grouped.put(length, new ArrayList<String>()); } grouped.get(length).add(name); }
使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); Map<Integer, List<String>> grouped = list.stream().collect(Collectors.groupingBy(name -> name.length()));
7.实现函数式接口
不使用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");
8.进行Optional操作
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<String> str = Optional.ofNullable("Hello World"); str.map(String::toUpperCase).ifPresent(System.out::println);
9.流水线操作
不使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); List<String> filteredList = new ArrayList<String>(); for (String name : list) { if (name.startsWith("a")) { filteredList.add(name.toUpperCase()); } } Collections.sort(filteredList);
使用lambda:
List<String> list = Arrays.asList("kk", "oneone", "11"); List<String> filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
10.线程创建
不使用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();