double sum = retIndustryPercentages.stream().mapToInt(RetIndustryPercentage::getCount).sum();2.Stream流转换为map
Map3. Stream流的filter使用map = retIndustryPercentages.stream().collect(Collectors.toMap(RetIndustryPercentage::getIndustry, RetIndustryPercentage::getCount)); 转换为对象: Map map = industryList.getData().stream().collect(Collectors.toMap(RetIndustry::getDisplayName, Function.identity()));
获得任意一个匹配的值 Optional4. Stream流的map使用result = words.parallel().filter(s->s.startWith("Q")).findAny(); 或者 Optional result = words.parallel().anyMatch(s->s.startWith("Q")); 获得第一个 Optional result = words.filter(s->s.startWith("Q")).findFirst();
Set5. Stream收集结果ids = data.stream().map(UserEntity::getId).collect(Collectors.toSet());
(1)获得指定返回类型的数组
String[] result = words.toArray(String[]::new);
(2)获得指定返回类型的HashSet
HashSetresult = stream.collect(HashSet::new,HashSet::add,HashSet::addAll);
(3)获得指定返回类型的set
Setids = data.stream().map(UserEntity::getId).collect(Collectors.toSet());
(4)获得指定返回类型的map
Mapmap = retIndustryPercentages.stream().collect(Collectors.toMap(RetIndustryPercentage::getIndustry, RetIndustryPercentage::getCount));
(5)获得指定返回拼接的字符串
String result = stream.map(Object::toString).collect(Collections.joining(","));
(6)获得总和,平均值,最值
(7)根据实体的字段属性去重
Listlist = result.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(RetElementTypeRole::getElementType))),ArrayList::new));
(8)将结果用逗号拼接字符串
auditTargetNodeEntity.setUsers(userIdList.stream().map(e -> e.toString()).collect(Collectors.joining(",")));
(9)将结果保存为集合
List6. Stream的分组分片userIds = new ArrayList(); reqStartFlow.getTargetNodeUsers().forEach(reqTargetNodeUser -> userIds.addAll(reqTargetNodeUser.getUserIds()));
(1)goupingBy分组
收集每个州所有城市的人口总数 Mapresult = cities.collect(groupingBy(City::getState,summingInt(City::getPopulation))); 收集每个州中人口最多的城市 Map result = cities.collect(groupingBy(City::getState,maxBy(Comparator.comparing(City::getPopulation)))); 收集每个州的城市的名称并按照其最大长度进行聚合 Map > result = cities.collect(groupingBy(City::getState,mapping(City::getName,maxBy(Comparator.comparing(String::length)))));
(2)reducing分片
收集每个州中的所有城市名称的字符串,名字之间用逗号分隔 Map二 NumberFormat的使用 1 百分数精度值格式化result = cities.collect(groupingBy(City::getState,reducing("",City::getName,(s,t)->s.length==0?t:s+", "+t))); 或者 Map result = cities.collect(groupingBy(City::getState,mapping(City::getName,joining(", ")))));
private static String formattedDecimalToPercentage(double decimal) {
//获取格式化对象
NumberFormat nt = NumberFormat.getPercentInstance();
//设置百分数精确度2即保留两位小数
nt.setMinimumFractionDigits(2);
return nt.format(decimal);
}
三 Collections集合的使用
1. 排序
Collections.sort(retMonthDocCounts, Comparator.comparing(RetMonthDocCount::getMonthStr)); 参数说明:集合,集合里的对象的属性进行排序四 Sql语句 1. 日期格式化
SELECt DATE_FORMAT((CURDATE() - INTERVAL 12 MONTH), '%Y-%m') monthStr,
count(DISTINCT (IF(industry IN (#{kecdl}, #{kacdl}), doc_no, NULL))) businessCount,
count(DISTINCT (IF(industry IN (#{gcjx}, #{gydl}, #{nyzbdl}), doc_no, NULL))) nonRoadCount,
count(DISTINCT doc_no) sumCount
FROM doc_file
WHERe deleted = FALSE
AND cancel_flag = false
AND `status` = 10
AND sub_category = '6'
AND industry IN (#{kecdl}, #{kacdl}, #{gcjx}, #{gydl}, #{nyzbdl})
AND created_at < CONCAt(DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m'), '-01')



