Java SE 8 学习笔记
一 Stream流的使用
1.Stream流获取总和
double sum = retIndustryPercentages.stream().mapToInt(RetIndustryPercentage::getCount).sum();
2.Stream流转换为map
Map<String, Integer> map = retIndustryPercentages.stream().collect(Collectors.toMap(RetIndustryPercentage::getIndustry, RetIndustryPercentage::getCount));
转换为对象:
Map<String, RetIndustry> map = industryList.getData().stream().collect(Collectors.toMap(RetIndustry::getDisplayName, Function.identity()));
3. Stream流的filter使用
获得任意一个匹配的值
Optional<String> result = words.parallel().filter(s->s.startWith("Q")).findAny();
或者
Optional<String> result = words.parallel().anyMatch(s->s.startWith("Q"));
获得第一个
Optional<String> result = words.filter(s->s.startWith("Q")).findFirst();
4. Stream流的map使用
Set<Long> ids = data.stream().map(UserEntity::getId).collect(Collectors.toSet());
5. Stream收集结果
(1)获得指定返回类型的数组
String[] result = words.toArray(String[]::new);
(2)获得指定返回类型的HashSet
HashSet<Stirng> result = stream.collect(HashSet::new,HashSet::add,HashSet::addAll);
(3)获得指定返回类型的set
Set<Long> ids = data.stream().map(UserEntity::getId).collect(Collectors.toSet());
(4)获得指定返回类型的map
Map<String, Integer> map = retIndustryPercentages.stream().collect(Collectors.toMap(RetIndustryPercentage::getIndustry, RetIndustryPercentage::getCount));
(5)获得指定返回拼接的字符串
String result = stream.map(Object::toString).collect(Collections.joining(","));
(6)获得总和,平均值,最值
(7)根据实体的字段属性去重
List<RetElementTypeRole> list = 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)将结果保存为集合
List<Long> userIds = new ArrayList();
reqStartFlow.getTargetNodeUsers().forEach(reqTargetNodeUser -> userIds.addAll(reqTargetNodeUser.getUserIds()));
6. Stream的分组分片
(1)goupingBy分组
收集每个州所有城市的人口总数
Map<String,Integer> result = cities.collect(groupingBy(City::getState,summingInt(City::getPopulation)));
收集每个州中人口最多的城市
Map<String,City> result = cities.collect(groupingBy(City::getState,maxBy(Comparator.comparing(City::getPopulation))));
收集每个州的城市的名称并按照其最大长度进行聚合
Map<String,Optional<String>> result = cities.collect(groupingBy(City::getState,mapping(City::getName,maxBy(Comparator.comparing(String::length)))));
(2)reducing分片
收集每个州中的所有城市名称的字符串,名字之间用逗号分隔
Map<String,String> result = cities.collect(groupingBy(City::getState,reducing("",City::getName,(s,t)->s.length==0?t:s+", "+t)));
或者
Map<String,String> result = cities.collect(groupingBy(City::getState,mapping(City::getName,joining(", ")))));
二 NumberFormat的使用
1 百分数精度值格式化
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')