实现一个List集合中元素的求和,平均值,最大最小值运算

查询所有用户年龄的总和

这里用到了java8里面collect将Stream转换成值,可以对集合元素进行基本的操作,求和操作如下:

List<User> userlist = userService.findAll();
Integer sum= userlist .stream().collect(Collectors.summingInt(User::getAge));

其他的操作:

函数解释
summingDouble 求和Stream的元素类型为double
summingIn 求和Stream的元素类型为int
summingLong 求和Stream的元素类型为long
summarizingDouble统计Stream的数据(double)状态,其中包括count,min,max,sum和平均
averagingDouble 求平均值Stream的元素类型为double
maxBy在指定条件下的,Stream的最大元素
minBy在指定条件下的,Stream的最小元素

使用:

List<Employee> emps = employeeService.getEmpByDepId(id);
		//取出上面集合中对象Employee的值Empid
		List<Integer> empids = emps.stream().map(Employee -> Employee.getEmpid()).collect(Collectors.toList());
		List<Examine> examines = ExamineService.getExamineWithIds(empids);
		/*//统计出该组成员所有成功工作量
		List<Integer> allSuccessNumberOfWork = examines.stream().map(Examine -> Examine.getSuccessNumberOfWork()).collect(Collectors.toList());
		//统计出该组成员所有失败工作量
		List<Integer> allFailNumberOfWork = examines.stream().map(Examine -> Examine.getFailNumberOfWork()).collect(Collectors.toList());
		//统计出该组员工所有人的最终考核,便于得出平均值与最高最低值
		List<Integer> allExamineWithDepId = examines.stream().map(Examine -> Examine.getExamine()).collect(Collectors.toList());*/
		//统计出该组成员所有成功工作量
		Integer sumSuccessNumberOfWork= examines .stream().collect(Collectors.summingInt(Examine::getSuccessNumberOfWork));
		//统计出该组成员所有失败工作量
		Integer sumFailNumberOfWork= examines .stream().collect(Collectors.summingInt(Examine::getFailNumberOfWork));
		//统计出该组员工所有人的最终考核,便于得出平均值与最高最低值
		Double averagingExamine = examines .stream().collect(Collectors.averagingInt(Examine::getExamine));
		/*查询出考核分最高的员工*/
		Examine maxExamineEmp = examines.stream().max(Comparator.comparing(Examine::getExamine)).get();
		/*Integer maxExamine = examines .stream().collect(Collectors.maxBy(Examine::getExamine));*/
		/*查询出最高考核分数是多少*/
		int maxExamine = examines.stream().max(Comparator.comparing(Examine::getExamine)).get().getExamine();
		/*查询出最低考核分数是多少*/
		int minExamine = examines.stream().min(Comparator.comparing(Examine::getExamine)).get().getExamine();
		return JsonMsg.success().addInfo("examines", examines);
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值