Java8 常用的Stream流操作

1.按照条件过滤集合

//type相同的则加入到新的集合
List<User> newList = list.stream().filter(User -> User.getType().equals(request.getType())).collect(Collectors.toList());

//删除元素
list.removeIf(s -> s.getType() == 3);

2.从集合中取出某个字段封装到新的集合

//写法一
List<String> names = list.stream().map(User::getName).collect(Collectors.toList());

//写法二
List<String> names = list.stream().map(s -> s.getName()).collect(Collectors.toList());

3.在list对象中取某两个属性转成map

//key无重复值
Map<Integer, String> map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getType));

//key有重复值的情况下
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));

4.转换集合中的对象

//将List<Commission>转换成List<CommissionVo>
List<Commission> list = new ArrayList<>();
List<CommissionVo> commissionVos = list.stream().map(value -> {
    CommissionVo commissionVo = new CommissionVo();
    BeanUtils.copyProperties(value, commissionVo);
    return commissionVo;
}).collect(Collectors.toList());

5.计算list对象中的某个属性

//集合中某个属性的和(BigDecimal类型)
BigDecimal couponMoneyTotal = redPackageCoupons.stream().map(RedPackageCoupon::getCouponMoney).reduce(BigDecimal::add).get();

//集合中某个属性的最大值
BigDecimal couponMoneyMax = redPackageCoupons.stream().max(Comparator.comparing(RedPackageCoupon::getCouponMoney)).get().getCouponMoney();

//集合中某个属性的最小值
Long couponDaysMin = redPackageCoupons.stream().min(Comparator.comparing(RedPackageCoupon::getCouponDays)).get().getCouponDays();

//根据某属性分组
Map<String,List<Apple>> map = appleList.stream().collect(Collectors.groupingBy(Apple::getType));

//根据某属性分组后,计算
Map<String, LongSummaryStatistics> collect = appleList.stream().collect(Collectors.groupingBy(Apple::getType,Collectors.summarizingLong(Apple::getCount)));
for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
    LongSummaryStatistics longSummaryStatistics = entry.getValue();
    System.out.println("--------key--------" + entry.getKey());
    System.out.println("求和:" + longSummaryStatistics.getSum());
    System.out.println("求平均" + longSummaryStatistics.getAverage());
    System.out.println("求最大:" + longSummaryStatistics.getMax());
    System.out.println("求最小:" + longSummaryStatistics.getMin());
    System.out.println("求总数:" + longSummaryStatistics.getCount());
}

6.循环list

//list.forEach()不能使用continue或者break
//写法一,适用于只有一行代码
list.forEach(productSku -> dataVoList.add(importDataVoMap.get(productSku)));

//写法二,适用于多行代码
list.forEach(value -> {
    //逻辑代码
});

7.分割list

//将list按每100个元素进行分割
List<List<String>> splitList = Lists.partition(list, 100);

8.一次向list集合中添加多个元素

//写法一
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5);

//写法二
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

9.对list进行截取、去重、排序

//截取,写法一
List<String> limitN = Stream.of("Monkey", "Lion", "Giraffe", "Lemur").limit(2).collect(Collectors.toList());

//写法二
List<String> skipN = Stream.of("Monkey", "Lion", "Giraffe", "Lemur").skip(2).collect(Collectors.toList());

//去重
List<String> uniqueAnimals = Stream.of("Monkey", "Lion", "Giraffe", "Lemur", "Lion").distinct().collect(Collectors.toList());

//排序
List<Integer> numbers = Arrays.asList(6, 2, 1, 4, 9);

//正序
numbers.sort(Comparator.naturalOrder());

//倒序
numbers.sort(Comparator.reverseOrder());

//对象list根据某个属性进行排序,正序
list.sort(Comparator.comparingInt(s -> s.getId));

//倒序
list.sort((((s1, s2) -> s2.getUserId() - s1.getUserId())));

10.判断某个值是否在list集合中的某个对象中存在

//判断某个值是否在list集合中的某个对象中存在
if (list.stream().anyMatch(s->s.getName().equals(""))){
	//存在
}

if (list.stream().noneMatch(s->s.getName().equals(""))){
	//不存在
}

11.集合与基本类型之间的转换

//List<Integer> 转换成 String,逗号隔开
List<Integer> list = Arrays.asList(1,2,3);
String collect = list.stream().map(String::valueOf).collect(Collectors.joining(","))
    
//String转换成List<Integer>
String str = "1,2,3";
List<Integer> list = Stream.of(str.split(",")).map(Integer::parseInt).collect(Collectors.toList())

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值