前几天公司接了一个大屏展示项目,多个维度数据共用了一张宽表,不同维度只是对数据做了不同得处理。为了减少数据库压力(不用数据库做计算),充分利用cpu资源,决定使用stream对数据处理。于是根据业务总结了以下常用stream操作,并封装成了工具类。
详情一、List
public static int flowSum(List
2.相乘并求和
public static double flowMulti(List
二、List
public static int flowFilterSum(List
2.过滤并求和——升级版(多个条件得过滤求和)
public static int flowFilterSum(List
三、List
public static List
2.两个字段相等过滤
public static List> flowFilterPlus(
List> flowList, String filterBy, String filterByPlus) {
return flowList.stream().filter(e -> MapUtils.getInteger(e, filterBy)
== MapUtils.getInteger(e, filterByPlus)).collect(Collectors.toList());
}
3.过滤
public static List> flowFilter(List> flowList,
String equal, String unequal, int condition) {
return flowList.stream().filter(e -> MapUtils.getInteger(e, equal) == condition
&& MapUtils.getInteger(e, unequal) != condition)
.collect(Collectors.toList());
}
四、List
1.分组求和
public static Map flowGroupSum(List> flowList,
String groupBy, String sumBy) {
Map analysis =
flowList.stream().collect(Collectors.groupingBy(obj ->
MapUtils.getInteger((Map) obj, groupBy)
, Collectors.summingLong(obj ->
MapUtils.getInteger((Map) obj, sumBy, 0))));
return analysis;
}
2.多条件分组求和
public static MapflowGroup(List > flowList, String sumBy, String... groupBy) { Map analysis = flowList.stream().collect(Collectors.groupingBy(obj -> { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < groupBy.length; i++) { if (i == groupBy.length - 1) { buffer.append(MapUtils.getString(obj, groupBy[i])); } else { buffer.append(MapUtils.getString(obj, groupBy[i]) + ":"); } } return buffer.toString(); } , Collectors.summingLong(obj -> MapUtils.getInteger((Map) obj, sumBy, 0)))); return analysis; }
3.分组过滤求和
public static Map flowFilterGroup(List> flowList,
String groupBy, String sumBy,String filterBy, int filterCondition) {
// 区县数据分组
Map analysis = flowList.stream().filter(obj -> {
return MapUtils.getInteger(obj, filterBy) == filterCondition;
}).collect(Collectors.groupingBy(obj -> MapUtils.getInteger((Map) obj, groupBy)
, Collectors.summingLong(obj -> MapUtils.getInteger((Map) obj, sumBy, 0))));
return analysis;
}
五、List
Map得key是根据params通过分隔符【:】拼接起来,确定数据得唯一
1.多条件去重
public static Set distinct(List> flowList,
String... groupBy) {
Set distinctSet = flowList.stream().map(obj -> {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < groupBy.length; i++) {
if (i == groupBy.length - 1) {
buffer.append(MapUtils.getString(obj, groupBy[i]));
} else {
buffer.append(MapUtils.getString(obj, groupBy[i]) + ":");
}
}
return buffer.toString();
}).collect(Collectors.toSet());
return distinctSet;
}
六、Map
Map得key是根据params通过分隔符【:】拼接起来得,此方法用于上一个方法去重后,格式化数据返回给前端。
public static List> mapTransList(Map flowMap,
String... params) {
List> returnList = flowMap.entrySet().stream().map(obj -> {
String[] temp = obj.getKey().split(":");
Map tempMap = new HashMap<>();
for (int i = 0; i < params.length; i++) {
tempMap.put(params[i], temp[i]);
}
return tempMap;
}).collect(Collectors.toList());
return returnList;
}
七、Set转List
public static List> split(Set set, String... groupBy) {
List> list = set.stream().map(obj -> {
String[] temp = obj.split(":");
Map tempMap = new HashMap<>();
for (int i = 0; i < temp.length; i++) {
tempMap.put(groupBy[i], temp[i]);
}
return tempMap;
}).collect(Collectors.toList());
return list;
}
八、计算百分比
没有用到stream,但由于大屏业务常用,因此记录下来
public static Map computerPer(Map groupMap,
int total, DecimalFormat df, int index) {
Map returnMap = new HashMap<>();
double countPer = 0.0;
// 保留两位小数
for (int i = 1; i <= index; i++) {
// 确保百分比之和为100
if (i == index) {
returnMap.put(i, Double.parseDouble(df.format(100 - countPer)));
} else {
double per = Double.parseDouble(df.format(groupMap.get(i) * 100.0 / total));
countPer += per;
returnMap.put(i, per);
}
}
return returnMap;
}



