最近有个需求是求一个List
ps: 这种方法求和 小数类型 会 丢失精度 怎么解决我没有研究哈
public static void main(String[] args) {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("aaa", 10);
map1.put("bbb", "20");
map1.put("ccc", 30.55);
map1.put("ddd", "30.55");
map1.put("eee", "");
Map<String, Object> map2 = new HashMap<>();
map2.put("aaa", 11);
map2.put("bbb", "21");
map2.put("ccc", 31.55);
map2.put("ddd", "31.55");
map2.put("eee", 0);
Map<String, Object> map3 = new HashMap<>();
map3.put("aaa", 12);
map3.put("bbb", "22");
map3.put("ccc", 32.55);
map3.put("ddd", "32.55");
map3.put("eee", 1.0);
Map<String, Object> map4 = new HashMap<>();
map4.put("aaa", 13);
map4.put("bbb", "23");
map4.put("ccc", 33.55);
map4.put("ddd", "33.55");
map4.put("eee", "");
Map<String, Object> map5 = new HashMap<>();
map5.put("aaa", 14);
map5.put("bbb", "24");
map5.put("ccc", 34.55);
map5.put("ddd", "34.55");
map5.put("eee", "3.002");
Map<String, Object> map6 = new HashMap<>();
map6.put("aaa", 15);
map6.put("bbb", "25");
map6.put("ccc", 35.55);
map6.put("ddd", "35.55");
map6.put("eee", "");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);
System.out.println("list : " + list);
double aavg = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("aaa")))).average().getAsDouble();
double amax = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("aaa")))).max().getAsDouble();
double amin = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("aaa")))).min().getAsDouble();
double asum = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("aaa")))).sum();
System.out.println("aavg:" + aavg + " ,amax:" + amax + ",amin:" + amin + ",asum:" + asum );
double bavg = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("bbb")))).average().getAsDouble();
double bmax = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("bbb")))).max().getAsDouble();
double bmin = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("bbb")))).min().getAsDouble();
double bsum = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("bbb")))).sum();
System.out.println("bavg:" + bavg + " ,bmax:" + bmax + ",bmin:" + bmin + ",bsum:" + bsum );
// 小数类型 求和会丢失精度
double cavg = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ccc")))).average().getAsDouble();
double cmax = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ccc")))).max().getAsDouble();
double cmin = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ccc")))).min().getAsDouble();
double csum = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ccc")))).sum();
System.out.println("cavg:" + cavg + " ,cmax:" + cmax + ",cmin:" + cmin + ",csum:" + csum );
double davg = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ddd")))).average().getAsDouble();
double dmax = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ddd")))).max().getAsDouble();
double dmin = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ddd")))).min().getAsDouble();
double dsum = list.stream().mapToDouble((s) -> Double.valueOf(String.valueOf(s.get("ddd")))).sum();
System.out.println("davg:" + davg + " ,dmax:" + dmax + ",dmin:" + dmin + ",dsum:" + dsum );
double eavg = list.stream().mapToDouble((s) ->
{
if(s.get("eee") != null) {
String eee = String.valueOf(s.get("eee"));
return StringUtils.isNotBlank(eee) && !("null".equalsIgnoreCase(eee)) ? Double.valueOf(eee) : 0;
}else {
return 0;
}
}
).average().getAsDouble();
double emax = list.stream().mapToDouble((s) ->
{
if(s.get("eee") != null) {
String eee = String.valueOf(s.get("eee"));
return StringUtils.isNotBlank(eee) && !("null".equalsIgnoreCase(eee)) ? Double.valueOf(eee) : 0;
}else {
return 0;
}
}
).max().getAsDouble();
double emin = list.stream().mapToDouble((s) ->
{
if(s.get("eee") != null) {
String eee = String.valueOf(s.get("eee"));
return StringUtils.isNotBlank(eee) && !("null".equalsIgnoreCase(eee)) ? Double.valueOf(eee) : 0;
}else {
return 0;
}
}
).min().getAsDouble();
double esum = list.stream().mapToDouble((s) ->
{
if(s.get("eee") != null) {
String eee = String.valueOf(s.get("eee"));
return StringUtils.isNotBlank(eee) && !("null".equalsIgnoreCase(eee)) ? Double.valueOf(eee) : 0;
}else {
return 0;
}
}
).sum();
System.out.println("eavg:" + eavg + " ,emax:" + emax + ",emin:" + emin + ",esum:" + esum );
}
}
执行结果
list : [{aaa=10, ccc=30.55, bbb=20, eee=, ddd=30.55}, {aaa=11, ccc=31.55, bbb=21, eee=0, ddd=31.55}, {aaa=12, ccc=32.55, bbb=22, eee=1.0, ddd=32.55}, {aaa=13, ccc=33.55, bbb=23, eee=, ddd=33.55}, {aaa=14, ccc=34.55, bbb=24, eee=3.002, ddd=34.55}, {aaa=15, ccc=35.55, bbb=25, eee=, ddd=35.55}]
aavg:12.5 ,amax:15.0,amin:10.0,asum:75.0
bavg:22.5 ,bmax:25.0,bmin:20.0,bsum:135.0
cavg:33.05 ,cmax:35.55,cmin:30.55,csum:198.29999999999998
davg:33.05 ,dmax:35.55,dmin:30.55,dsum:198.29999999999998
eavg:0.6669999999999999 ,emax:3.002,emin:0.0,esum:4.002
浮点类型 求和会 出现精度丢失,
.mapToDouble() 括号里面要返回对应的数值类型,否则会报错,也可以直接应用于 javaBaen 的集合,将对应的属性放入表达式里面就好了
参考博客
https://www.cnblogs.com/aitree/p/14301533.html