Java8中常用的stream流处理集合转换方法!

一、List转Map

Map<String, String> collect = list.stream().collect(Collectors.toMap(UserEntity::getUserName, UserEntity::getEmail));

二、List转List

List<String> collect = userList.stream().map(UserEntity::getuserName).collect(Collectors.toList());

三、分组

Map<String, List<UserEntity>> listMap = list.stream().collect(Collectors.groupingBy(UserEntity::getUserName));
for(String key:listMap.keySet()){
        System.out.print("姓名:"+key);
        listMap.get(key).forEach(UserEntity -> System.out.print(UserEntity.getEmail()));
        System.out.println();
        }

四、排序

list.stream().sorted(Comparator.comparing(UserEntity-> UserEntity.getAge()))
        .forEach(UserEntity -> System.out.println(UserEntity.getUserName()));

五、过滤

list.stream().filter(UserEntity -> UserEntity.getSex().equals("男")).collect(Collectors.toList())
        .forEach(UserEntity -> System.out.println(UserEntity.getUserName()));

六、多条件去重

list.stream().collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(
        Comparator.comparing(UserEntity -> UserEntity.getAge() + ";" + UserEntity.getUserName()))), ArrayList::new))
        .forEach(UserEntity -> System.out.println(UserEntity.getUserName()));

七、最小值

Integer min = list.stream().mapToInt(UserEntity::getAge).min().getAsInt();

七、最大值

Integer max = list.stream().mapToInt(UserEntity::getAge).max().getAsInt();

八、平均值

Double average = list.stream().mapToInt(UserEntity::getAge).average().getAsDouble();

八、求和

Integer sum = list.stream().mapToInt(UserEntity::getAge).sum();

九、分组求和

Map<String, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(UserEntity::getSex, Collectors.summarizingInt(UserEntity::getAge)));
IntSummaryStatistics statistics1 = collect.get("男");
IntSummaryStatistics statistics2 = collect.get("女");
System.out.println(statistics1.getSum());
        System.out.println(statistics1.getAverage());
        System.out.println(statistics1.getMax());
        System.out.println(statistics1.getMin());
        System.out.println(statistics1.getCount());
        System.out.println(statistics2.getSum());
        System.out.println(statistics2.getAverage());
        System.out.println(statistics2.getMax());
        System.out.println(statistics2.getMin());
        System.out.println(statistics2.getCount());

十、将 List元素存储到数组中

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
int[] arr = list.stream().mapToInt(Integer::intValue).toArray();

十一、将数组元素存储到 List 中

int[] arr = {1, 2, 3, 4, 5};
List<Integer> list = IntStream.of(arr).boxed().collect(Collectors.toList());

十二、两个集合间的交集

List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());

十三、两个集合间的差集 (list1 - list2)

List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());

十四、两个集合间的差集 (list2 - list1)

List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());

十五、两个集合间的并集


List<String> listAll = list1.parallelStream().collect(Collectors.toList());
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);

十六、两个集合间的去重并集

List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());

十七、两个集合间的根据id去重

allRoleList.stream().filter(distinctByKey(e -> e.getId())).collect(Collectors.toList());

十八、Set

Set<String> collect = userList.stream().map(UserEntity::getuserName).collect(Collectors.toSet());

未完待续......

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值