java 使⽤jdk8的Stream 来获取list 集合的最⼩值、最⼤值、总和、平均数java使⽤jdk8的Stream来获取list集合的最⼩值、最⼤值、总和、平均数
User 实体类package ;
import Data ;import BigDecimal ;import LocalDate ;import List ;/** * @author qzz */@Data public class User { /** * id */ private Integer id ; /** * 姓名 */ private String name ; /** * ⼯号 */ private String jobNumber ; /** * 年龄 */ private Integer age ; /** * true:男 false :⼥ */ private Boolean gender ; /** * ⾝⾼ */ private Double height ; /** * 出⽣⽇期 */ private LocalDate birthday ; /** * 成员数量 */ private BigDecimal familyMemberQuantity ; public User (String name , String jobNumber , Integer age , Boolean gender , Double height , LocalDate birthday ){ this .name = name ; this .jobNumber = jobNumber ; this .gender = gender ; this .age = age ; this .height = height ; this .birthday = birthday ; } public User (String name ,Integer age ,BigDecimal familyMemberQuantity ){ this .name = name ; this .age = age ; this .familyMemberQuantity = familyMemberQuantity ;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
⼀、获取list 集合的最⼩值、最⼤值
} /** * 输出打印信息 * @param list */ public static void printUsers (List <User > list ){ System .out .println ("[姓名]\t\t[⼯号]\t\t[性别]\t\t[年龄]\t\t[⾝⾼]\t\t[⽣⽇]"); System .out .println ("-----------------------------------------------------------------------"); list .forEach (u -> System .out .println (printValue (u ))); System .out .println (" "); } /*** * 输出list 结果集 * @param user * @return */ public static String printValue (User user ){ String str =String .format ("%s\t\t\t%s\t\t%s\t\t%s\t\t\t%s\t\t%s", user .name ,user .jobNumber ,user .gender .toString (),user .age .toString (), user .height .toString (),user .birthday .toString ()); return str ; }}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82package ;import User ;import BigDecimal ;import ArrayList ;import List ;/** * List 集合最值:使⽤jdk8的Stream 来获取list 集合的最⼩值、最⼤值、总和、平均数 * @author qzz */public class ListMaxMin { public static void main (String [] args ) { //构建测试数据 List <User > list = n
ew ArrayList <User >(); list .add (new User ("张丹",11,new BigDecimal (11))); list .add (new User ("刘⼤",13,new BigDecimal (13))); list .add (new User ("飒飒",16,new BigDecimal (16))); list .add (new User ("斯蒂芬",11,new BigDecimal (11))); //获取⽤户年龄的最⼤、最⼩、总和、平均值 int max = list .stream ().mapToInt (u ->u .getAge ()).max ().getAsInt (); int min = list .stream ().mapToInt (u ->u .getAge ()).min ().getAsInt (); System .out .println ("年龄最⼤值:"+max +"\n 年龄最⼩值:"+min ); }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18java stream
19
20
21
22
23
24
25
26
27
28
29
30
运⾏结果:
⼆、获取list 集合的总和、平均数
运⾏结果:
三、⽰例完整代码package ;import User ;import BigDecimal ;import ArrayList ;import List ;/** * List 集合最值:使⽤jdk8的Stream 来获取list 集合的最⼩值、最⼤值、总和、平均数 * @author qzz */public class ListMaxMin { public static void main (String [] args ) { //构建测试数据 List <User > list = new ArrayList <User >(); list .add (new User ("张丹",11,new BigDecimal (11)));
list .add (new User ("刘⼤",13,new BigDecimal (13))); list .add (new User ("飒飒",16,new BigDecimal (16))); list .add (new User ("斯蒂芬",11,new BigDecimal (11))); //获取⽤户年龄的最⼤、最⼩、总和、平均值 //求和 //求和:分基本类型和⼤数据类型,基本类型先mapToInt ⽅法,然后调⽤sun ⽅法;⼤数类型使⽤reduce 调⽤BigDecimal::add ⽅法 //基本类型求和 int sum = list .stream ().mapToInt (u ->u .getAge ()).sum (); //BigDecimal 求和 BigDecimal totalQuantity = list .stream ().map (u ->u .getFamilyMemberQuantity ()).reduce (BigDecimal .ZERO ,BigDecimal ::add ); double avg = list .stream ().mapToInt (u ->u .getAge ()).average ().getAsDouble (); System .out .println ("年龄总和:"+sum +"\n 年龄平均值:"+avg ); System .out .println ("成员数量总和:"+totalQuantity ); }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package ;import User ;import BigDecimal ;import ArrayList ;import List ;/** * List 集合最值:使⽤jd
k8的Stream 来获取list 集合的最⼩值、最⼤值、总和、平均数 * @author qzz */public class ListMaxMin { public static void main (String [] args ) { //构建测试数据 List <User > list = new ArrayList <User >(); list .add (new User ("张丹",11,new BigDecimal (11))); list .add (new User ("刘⼤",13,new BigDecimal (13))); list .add (new User ("飒飒",16,new BigDecimal (16))); list .add (new User ("斯蒂芬",11,new BigDecimal (11))); //获取⽤户年龄的最⼤、最⼩、总和、平均值 int max = list .stream ().mapToInt (u ->u .getAge ()).max ().getAsInt (); int min = list .stream ().mapToInt (u ->u .getAge ()).min ().getAsInt (); //求和 //求和:分基本类型和⼤数据类型,基本类型先mapToInt ⽅法,然后调⽤sun ⽅法;⼤数类型使⽤reduce 调⽤BigDecimal::add ⽅法 //基本类型求和 int sum = list .stream ().mapToInt (u ->u .getAge ()).sum (); //BigDecimal 求和 BigDecimal totalQuantity = list .stream ().map (u ->u .getFamilyMemberQuantity ()).reduce (BigDecimal .ZERO ,BigDecimal ::add ); double avg = list .stream ().mapToInt (u ->u .getAge ()).average ().getAsDouble (); System .out .println ("年龄最⼤值:"+max +"\n 年龄最⼩值:"+min ); System .out .println ("年龄总和:"+sum +"\n 年龄平均值:"+avg ); System .out .println ("成员数量总和:"+totalQuantity ); }}12345678910111213141516171819202122232425262728293031323334353637383940
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论