Java SE 8 学习笔记

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 &lt; CONCAT(DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m'), '-01')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值