一、lambda方法,如下:
IntSummaryStatistics statistics= (IntSummaryStatistics) list
.stream()
.collect(
Collectors.summarizingInt(
e->Integer.valueOf( ((Map) e).get("xxx Key对象").toString() )));
具体方法:我们用以上得到的结果 .get,如下图,可以直接获取集合的和,或平均值、最大值、最小值
二、以下是代码测试案例:
package demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author :zkx
* @createtime :2020年5月30日 上午10:43:57
* @description:
*/
public class Test02 {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Exception {
//创建map集合
Map map1 = new HashMap();
Map map2 = new HashMap();
Map map3 = new HashMap();
Map map4 = new HashMap();
map1.put("id", "100");
map1.put("number", 1);
map2.put("id", "123");
map2.put("number", 2);
map3.put("id", "100");
map3.put("number", 3);
map4.put("id", "100");
map4.put("number", 4);
//创建list集合
List list = new ArrayList();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
System.out.println("list集合数据: " + list.toString());
//lambda方法
IntSummaryStatistics sumcc2 = (IntSummaryStatistics) list.stream().collect(
Collectors.summarizingInt(e->Integer.valueOf( ((Map) e).get("id").toString() )));
System.out.println("lambda方法返回的数据: " + sumcc2);
long count = sumcc2.getCount();
System.out.println("统计条数为: " + count);
double average = sumcc2.getAverage();
System.out.println("平均值为: " + average);
int max = sumcc2.getMax();
System.out.println("最大值为: " + max);
int min = sumcc2.getMin();
System.out.println("最小值为: " + min);
long sum = sumcc2.getSum();
System.out.println("和为: " + sum);
}
}
三、效果图如下:
版权声明:本文为JavaWebEngineer原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。