Java8 List统计(最大值,最小值平均值总和)

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8 List统计(最大值,最小值平均值总和)相关的知识,希望对你有一定的参考价值。

核心功能代码:

        Integer sum = list.stream().mapToInt(User::getAge).sum();
        System.out.println("年龄总和:" +sum);
        Integer max = list.stream().mapToInt(User::getAge).max().orElse(0);
        System.out.println("年龄 最大:" + max);
        Integer min = list.stream().mapToInt(User::getAge).min().orElse(0);
        System.out.println("年龄 最小:" + min);
        Double average = list.stream().mapToInt(User::getAge).average().orElse(0l);
        System.out.println("年龄 平均:" + df.format(average));

Java8 Stream除了支持Int类型,还支持double 类型和long类型

 学生年龄实例:

package com.digipower.test;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ListTest 
	private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点

	public static void main(String[] args) 
		Random random = new Random();
		List<User> list = new ArrayList<User>();
		for (int i = 1; i <= 5; i++) 
			int age = random.nextInt(10) + 5;// 随机年龄:10-15
			User u = new User(String.valueOf(i), "用户-" + i, age);
			list.add(u);
		
		Integer sum = list.stream().mapToInt(User::getAge).sum();
        System.out.println("年龄总和:" +sum);
        Integer max = list.stream().mapToInt(User::getAge).max().orElse(0);
        System.out.println("年龄 最大:" + max);
        Integer min = list.stream().mapToInt(User::getAge).min().orElse(0);
        System.out.println("年龄 最小:" + min);
        Double average = list.stream().mapToInt(User::getAge).average().orElse(0l);
        System.out.println("年龄 平均:" + df.format(average));
	

	static class User 
		private String id;
		private String name;
		private int age;

		public String getId() 
			return id;
		

		public void setId(String id) 
			this.id = id;
		

		public String getName() 
			return name;
		

		public void setName(String name) 
			this.name = name;
		

		public int getAge() 
			return age;
		

		public void setAge(int age) 
			this.age = age;
		

		public User(String id, String name, int age) 
			super();
			this.id = id;
			this.name = name;
			this.age = age;
		

	


效果截图:

溫馨提示:針對金融数据类型BigDecimal,求和方式必须使用Stream 类中的reduce函数

学生余额实例:

package com.digipower.test;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

public class ListTest 
	private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点

	public static void main(String[] args) 
		Random random = new Random();
		List<User> list = new ArrayList<User>();
		for (int i = 1; i <= 5; i++) 
			int age = random.nextInt(10) + 5;// 随机年龄:10-15
			User u = new User(String.valueOf(i), "用户-" + i, age);
			double balance = random.nextDouble() * 100 + 100;//随机金额:100-200
			u.setBalance(new BigDecimal(balance).setScale(BigDecimal.ROUND_HALF_UP, 2));
			list.add(u);
		
		Integer sum = list.stream().mapToInt(User::getAge).sum();
        System.out.println("年龄总和:" +sum);
        Integer max = list.stream().mapToInt(User::getAge).max().orElse(0);
        System.out.println("年龄 最大:" + max);
        Integer min = list.stream().mapToInt(User::getAge).min().orElse(0);
        System.out.println("年龄 最小:" + min);
        Double average = list.stream().mapToInt(User::getAge).average().orElse(0l);
        System.out.println("年龄 平均:" + df.format(average));
        
        BigDecimal add = list.stream().map(User::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("金额 总和:" + df.format(add));
        Optional<User> maxBalance = list.stream().max((u1, u2) -> u1.getBalance().compareTo(u2.getBalance()));
        System.out.println("金额 最高:" + df.format(maxBalance.get().getBalance()));
        Optional<User> minBalance = list.stream().min((u1, u2) -> u1.getBalance().compareTo(u2.getBalance()));
        System.out.println("金额 最低:" + df.format(minBalance.get().getBalance()));
	

	static class User 
		private String id;
		private String name;
		private int age;
		private BigDecimal balance;

		public String getId() 
			return id;
		

		public void setId(String id) 
			this.id = id;
		

		public String getName() 
			return name;
		

		public void setName(String name) 
			this.name = name;
		

		public int getAge() 
			return age;
		

		public void setAge(int age) 
			this.age = age;
		
		
		

		public BigDecimal getBalance() 
			return balance;
		

		public void setBalance(BigDecimal balance) 
			this.balance = balance;
		

		public User(String id, String name, int age) 
			super();
			this.id = id;
			this.name = name;
			this.age = age;
		

	


效果截图

 

以上是关于Java8 List统计(最大值,最小值平均值总和)的主要内容,如果未能解决你的问题,请参考以下文章

java8 list统计(求和最大最小平均)

java8 arraylist操作 汇)- Java8 Lambda list统计(求和最大最小平均)

JAVA8 List最大值最小值求和平均值以及排序

Java8常见操作整理

利用stream对list集合中的bigdecimal进行分组求和,均值,最大值,最小值

MySQL的聚合函数