Java 8 中的 Streams API Demo

 

Java8 Streams demo

 

一、获取List集合中的某个字段的List集合

例如:

List<Long> userIdList = payBillVOList.stream().mapToLong(PayBillVO::getUserId).boxed().collect(Collectors.toList());

before java8 :

 

 

List<Long> userIdList = new ArrayList<>();
for (PayBillVO payBillVO : payBillVOList) {
    userIdList.add(payBillVO.getUserId());
} 

 

List集合中的某个字段转为String

 

String appIds = Joiner.on(",").join(deviceUserDOList.stream().map(TDeviceUserDO::getAppId).distinct().collect(Collectors.toList()));

 

 

 

 

 

 

二、List对象转Map

例如:

单个对象:

 

Map<Long, UserMain> userMainMap = userMainList.stream().collect(Collectors.toMap(UserMain::getUserId, c -> c));

 

 

 

List对象:

 

Map<String, List<CollectionVO>> collectionVOListMap = collectionVOList.stream().collect(Collectors.groupingBy(CollectionVO::getCreateDate));

 

before java8 :

自己写很复杂。

三、String按特定的规则转List

例如:

基础版本:withdrawInfoIds=110,120

 

List<Long> withdrawInfoIdList = Splitter.on(",").splitToList(withdrawInfoIds).stream().mapToLong(Long::valueOf).boxed().collect(Collectors.toList());
List<Integer> paymentMethodList = Arrays.stream("1,5,6,7".split(",")).map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList());

兼容版本:relationMobileIds=110,,120,

 

List<Long> relationIdList = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(relationMobileIds).stream().mapToLong(Long::valueOf).boxed().collect(Collectors.toList());

 

 

 

before java8 :

 

List<Long> withdrawInfoIdList = new ArrayList<>();
String[] withdrawInfoIdArr = withdrawInfoIds.split(",");
for (String id : withdrawInfoIdArr) {
      withdrawInfoIdList.add(Long.valueOf(id));
} 

 

四、获取list集合中某个字段出现的总数(例如统计用户ID出现的次数)

例如:

 

Map<Long, Long> withdrawInfoListMap = withDrawInfoDOList.stream().collect(Collectors.groupingBy(TWithDrawInfoDO::getUserId, Collectors.counting()));

 

 

五、List集合中的对象转为另一个对象

例如:

 

List<EntryVO> entryAPIList = entryDOList.getContent().stream().map(EntryVO::toVoFromDo).collect(Collectors.toList());


其中:

 

 

    public static EntryVO toVoFromDo(EntryDO entryDO){
        if (entryDO == null){
            return null;
        }
        return new EntryVO(entryDO.getAutoId(), entryDO.getUserId());
    }

 

六、从list集合中筛选出符合条件的一条记录

 

例如:

 

Long entryId = 8;
EntryAPI entryAPI = entryAPIList.stream().filter(p -> p.getAutoId().equals(entryId)).findFirst().orElse(null);


七、排序

 

 

 

例如:先排序,再格式化输出
List<Date> openTimeList;
List<String> openTimeStringList = openTimeList.stream().sorted(Comparator.comparing(date -> date)).map((date)-> DateUtils.formatHourMinutes(date)).collect(Collectors.toList());


例如:把list集合中的数据转成String输出 空格隔开

 

 

List<String> openTimeStringList 
String openTimeString = openTimeStringList.stream().collect(Collectors.joining("  "));

八、求和

Integer point = billPointsDOList.stream().mapToInt(TBillPointsDO::getTotalPointsAvaliable).sum();
BigDecimal paidAmount = billPaymentDOList.stream().map(TBillPaymentDO::getPayment).reduce(BigDecimal.ZERO, BigDecimal::add);

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值