Java8 新特性学习 Lambda表达式 和 Stream 用法案例
学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html
1.使用lambda代理匿名内部类
//不用lambda
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("java 8 lambda 匿名类");
}
}).start();
//使用lambda
new Thread(()->{
System.out.println("java 8 lambda 匿名类");
}).start();
输出结果:
java 8 lambda 匿名类
java 8 lambda 匿名类
2.使用lambda表达式对列表进行迭代
//不用lambda
List<String> strings = Arrays.asList("java", "go", "python", "php");
for (String string : strings) {
System.out.print(string+"\t");
}
System.out.println();
//使用lambda
strings.forEach((str) -> System.out.print(str+"\t"));
输出结果
java go python php
java go python php
3.使用lambda表达式和函数式接口Predicate
//不用lambda表达式
ArrayList<User> users = new ArrayList<>();
users.add(new User("xiaoming",23));
users.add(new User("daming",26));
Collections.sort(users, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getAge().compareTo(o2.getAge());
}
});
for (User user : users) {
System.out.println(user);
}
//使用lambda表达式
Collections.sort(users,(o1,o2)->{
return o1.getAge().compareTo(o2.getAge());
});
users.forEach(System.out::println);
输出结果:
User{name='xiaoming', age=23}
User{name='daming', age=26}
User{name='xiaoming', age=23}
User{name='daming', age=26}
4.使用lambda表达式,并且加入多个Predicate
// 多个Predicate 组合使用 and or
List<String> languages2 = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
Predicate<String> predicate1 = (n) -> n.startsWith("S");
Predicate<String> predicate2 = (m) -> m.endsWith("l");
languages2.stream().filter(predicate1.or(predicate2)).forEach((n) -> {
System.out.println(n);
});
输出结果:
Java
Scala
Haskell
5.使用lambda表达式的Map示例
// 函数式编程 map
//不适用lambda表达式
Integer[] price = {100,200,300,400};
List<Integer> prices = Arrays.asList(price);
for (Integer dou : prices) {
System.out.println(dou + .12 * dou);
}
System.out.println("使用lambda:");
prices.stream().map((num) -> (.12*num+num)).forEach((n)->{
System.out.println(n);
});
输出结果:
112.0
224.0
336.0
448.0
使用lambda:
112.0
224.0
336.0
448.0
1120.0
6.使用lambda表达式的Reduce示例
// 函数式编程 reduce 将所有的值合并
//不适用lambda
Integer[] price2 = {100,200,300,400};
List<Integer> integers2 = Arrays.asList(price);
Double total = 0d;
for (Integer integer : integers2) {
total += integer + .12 * integer;
}
System.out.println(total);
System.out.println("使用lambda");
Double aDouble = integers2.stream().map((num) -> (num + .12 * num)).reduce((total1, total2) -> total1 + total2).get();
System.out.println(aDouble);
输出结果:
1120.0
使用lambda
1120.0
7.通过过滤创建一个String列表
List<String> languages3 = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
List<String> collect = languages3.stream().filter(x -> x.length() > 4).collect(Collectors.toList());
System.out.println(collect);
输出结果:
[Scala, Haskell]
8.对列表的每个元素应用函数
// collect(Collectors.joining(", "));将字符串根据规则转换
List<String> languages4 = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
String collect2 = languages4.stream().map(x -> (x.toUpperCase())).collect(Collectors.joining(","));
System.out.println(collect2);
输出结果:
JAVA,SCALA,C++,HASKELL,LISP
9.distinct() 去重
// distinct() 去重
Integer[] aaa = {1,1,2,2,3,3,4,5,6,1,23,44,32};
List<Integer> integers1 = Arrays.asList(aaa);
List<Integer> collect3 = integers1.stream().distinct().collect(Collectors.toList());
System.out.println(collect3);
输出结果:
[1, 2, 3, 4, 5, 6, 23, 44, 32]
10.计算最大值最小值总和 平均值
// 计算最大值最小值总和 平均值
IntSummaryStatistics intSummaryStatistics = integers1.stream().mapToInt(x -> x).summaryStatistics();
System.out.println(intSummaryStatistics.getCount());
System.out.println(intSummaryStatistics.getAverage());
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getMin());
System.out.println(intSummaryStatistics.getSum());
输出结果:
13
9.76923076923077
44
1
127