Java8中Lambda表达式使用和Stream API详解
前言
java8 的新特性:lambda表达式、强大的 stream api、全新时间日期 api、concurrenthashmap、metaspace。总得来说,java8 的新特性使 java 的运行速度更快、代码更少、便于并行、最大化减少空指针异常。
0x00. 前置数据
private list<people> peoples = null; @beforeeach void before () { peoples = new arraylist<>(); peoples.add(new people("k.o1", 21, new date())); peoples.add(new people("k.o3", 23, new date())); peoples.add(new people("k.o4", 24, new date())); peoples.add(new people("k.o5", 25, new date())); peoples.add(new people("k.o2", 22, new date())); peoples.add(new people("k.o6", 26, new date())); }
0x01. 提取对象中的一列
/** * 提取1列 */ @test void whenextractcolumnsuccess () { //第一种写法 list<integer> ages1 = peoples.stream().map(people -> people.getage()).collect(collectors.tolist()); system.out.println("###println: args1----"); ages1.foreach(system.out::println); //简单一点的写法 list<integer> ages2 = peoples.stream().map(people::getage).collect(collectors.tolist()); system.out.println("###println: args2----"); ages1.foreach(system.out::println); }
###println: args1----
21
22
23
24
25
26
###println: args2----
21
22
23
24
25
26
/** * 只要年纪大于25岁的人 */ @test void whenfilteragegt25success () { list<people> peoples1 = peoples.stream().filter(x -> x.getage() > 25).collect(collectors.tolist()); peoples1.foreach(x -> system.out.println(x.tostring())); }
people{name='k.o6', age=26, birthday=wed may 15 22:20:22 cst 2019}
0x03. 列表中对象数值型列数据求和
/** * 求和全部年纪 */ @test void sumallpeopleagesuccess () { integer sum1 = peoples.stream().collect(collectors.summingint(people::getage)); system.out.println("###sum1: " + sum1); integer sum2 = peoples.stream().maptoint(people::getage).sum(); system.out.println("###sum2: " + sum2); }
###sum1: 141
###sum2: 141
0x04. 取出集合符合条件的第一个元素
/** * 取出年纪为25岁的人 */ @test void extractageeq25success () { optional<people> optionalpeople = peoples.stream().filter(x -> x.getage() == 25).findfirst(); if (optionalpeople.ispresent()) system.out.println("###name1: " + optionalpeople.get().getname()); //简写 peoples.stream().filter(x -> x.getage() == 25).findfirst().ifpresent(x -> system.out.println("###name2: " + x.getname())); }
###name1: k.o5
###name2: k.o5
0x05. 对集合中对象字符列按规则拼接
/** * 逗号拼接全部名字 */ @test void printallnamesuccess () { string names = peoples.stream().map(people::getname).collect(collectors.joining(",")); system.out.println(names); }
k.o1,k.o2,k.o3,k.o4,k.o5,k.o6
0x06. 将集合元素提取,转为map
/** * 将集合转成(name, age) 的map */ @test void list2mapsuccess () { map<string, integer> map1 = peoples.stream().collect(collectors.tomap(people::getname, people::getage)); map1.foreach((k, v) -> system.out.println(k + ":" + v)); system.out.println("--------"); //(name object) map<string, people> map2 = peoples.stream().collect(collectors.tomap(people::getname, people::getthis)); map2.foreach((k, v) -> system.out.println(k + ":" + v.tostring())); } //people中自己实现的方法 public people getthis () { return this; }
k.o2:22
k.o3:23
k.o1:21
k.o6:26
k.o4:24
k.o5:25
--------
k.o2:people{name='k.o2', age=22, birthday=wed may 15 22:42:39 cst 2019}
k.o3:people{name='k.o3', age=23, birthday=wed may 15 22:42:39 cst 2019}
k.o1:people{name='k.o1', age=21, birthday=wed may 15 22:42:39 cst 2019}
k.o6:people{name='k.o6', age=26, birthday=wed may 15 22:42:39 cst 2019}
k.o4:people{name='k.o4', age=24, birthday=wed may 15 22:42:39 cst 2019}
k.o5:people{name='k.o5', age=25, birthday=wed may 15 22:42:39 cst 2019}
0x07. 按集合某一属性进行分组
/** * 按名字分组 */ @test void listgroupbynamesuccess() { //添加一个元素方便看效果 peoples.add(new people("k.o1", 29, new date())); map<string, list<people>> map = peoples.stream().collect(collectors.groupingby(people::getname)); map.foreach((k, v) -> system.out.println(k + ":" + v.size())); }
k.o2:1
k.o3:1
k.o1:2
k.o6:1
k.o4:1
k.o5:1
0x08. 求集合对象数值列平均数
/** * 求人平均年龄 */ @test void averagingagesuccess () { double avgage = peoples.stream().collect(collectors.averagingint(people::getage)); system.out.println(avgage); }
23.5
0x09. 对集合按某一列排序
/** * 按年龄排序 */ @test void sortbyagesuccess () { system.out.println("###排序前---"); peoples.foreach(x -> system.out.println(x.getage())); peoples.sort((x, y) -> { if (x.getage() > y.getage()) { return 1; } else if (x.getage() == y.getage()) { return 0; } return -1; }); system.out.println("###排序后---"); peoples.foreach(x -> system.out.println(x.getage())); }
###排序前---
21
23
24
25
22
26
###排序后---
21
22
23
24
25
26
未完待续
<源码地址:https://github.com/cos2a/learning-repo/tree/master/core-java8>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
下一篇: SPI