参考Java中Lambda表达式的使用、Java8 lambda表达式10个示例
package com.fhc.modules.api.utils;
import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* lambda Test
*/
public class java8lambda {
public static void main(String[] args) {
/** 使用Lambda表达式和函数接口 */
List languages = Arrays.asList("Python", "Java", "C++", "JavaScript", "Vue");
System.out.println("Languages which starts with J:");
filter(languages, str -> str.toString().startsWith("J"));
System.out.println("Languages whiche end with a");
filter(languages, str -> str.toString().endsWith("a"));
/** 复杂的结合Predicate使用 */
System.out.println("Languages whiche starts with J and end with a:");
Predicate startWithJ = str -> str.toString().startsWith("J");
Predicate endWithJ = str -> str.toString().endsWith("a");
filter(languages, startWithJ.and(endWithJ)); // 等价于下
filter(languages, str -> str.toString().startsWith("J") && str.toString().endsWith("a"));
System.out.println("Languages whiche starts with J or end with a:");
filter(languages, startWithJ.or(endWithJ)); // 等价于下
filter(languages, str -> str.toString().startsWith("J") || str.toString().endsWith("e"));
/** 使用Lambda 实现 Map 和 Reduce */
System.out.println("使用Lambda 实现 Map 和 Reduce ");
List constBeforeTax = Arrays.asList(100, 200, 300, 400, 500, 400);
for (Integer cost : constBeforeTax) {
double price = cost +.12 * cost;
System.out.println(price);
}
System.out.println("总个数:" + constBeforeTax.stream().count());
System.out.println("去重后个数:" + constBeforeTax.stream().distinct().count());
System.out.println("取第一个:" + constBeforeTax.stream().findFirst().get());
constBeforeTax.stream().map(cost -> cost +.12 * cost).forEach(System.out::println);
// reduce()是将集合中所有值结合进一个,Reduce类似SQL语句中的sum(),avg()或count()
System.out.println("Toal:" + constBeforeTax.stream().map(cost -> cost +.12 * cost).reduce((sum, cost) -> sum + cost).get());
// System.out.println("Toal:" + constBeforeTax.stream().map(cost -> cost + .12 * cost).reduce((, cost) -> avg).get());
/** 通过filter创建一个字符串String的集合 */
List filterStr = languages.stream().filter(str -> str.toString().startsWith("J") || str.toString().endsWith("e")).collect(Collectors.toList());
filter(filterStr, str ->true);
/** 对集合中每个元素应用函数 */
String filterString = filterStr.stream().map(str -> str.toUpperCase()).collect(Collectors.joining(","));
System.out.println(filterString);
/** sum 、avg 、max 、min */
List primes = Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9);
IntSummaryStatistics intSummaryStatistics = primes.stream().mapToInt(s -> s).summaryStatistics();
System.out.println("sum:" + intSummaryStatistics.getSum() +"\navg:" + intSummaryStatistics.getAverage() +"\nmax:" + intSummaryStatistics.getMax() +"\nmin:" + intSummaryStatistics.getMin());
System.out.println(primes.parallelStream().mapToInt(s -> s).sum());
/** 排序 */
String[] names = {"Python", "Java", "C++", "JavaScript", "Vue"};
Arrays.sort(names, new Comparator() {
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
});
Arrays.sort(names, (String s1, String s2) -> s1.compareTo(s2));
}
public static void filter(List names, Predicate condition) {
names.forEach(name -> {
if (condition.test(name)) System.out.print(name +"|||");
});
System.out.println();
}
}