list集合中stream流的使用

package com.ehualu.data.business.listTest.service;

import com.ehualu.data.business.listTest.model.PersonModel;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ListTestService {
    public static void main(String[] args) {
        ArrayList<String> strList = new ArrayList<>();
        strList.add("张三");
        strList.add("李四");
        strList.add("张三");
        strList.add("王五");

        // 找寻list中包含三的元素
        List<String> strListColl = strList.stream()
                .filter((a) -> a.startsWith("张三"))
                .collect(Collectors.toList());
        // System.out.println("strListColl.size() = " + strListColl.size());
        // System.out.println("strListColl.toString() = " + strListColl.toString());

        // 利用stream流遍历list集合
        // strList.stream().forEach((a) -> System.out.println("a = " + a));

        // 过滤掉不符合条件的元素 fiter 新的集合将会只保留符合条件的元素
        // List<String> fiterList = strList.stream().filter((a) -> a.equals("李四")).collect(Collectors.toList());
        // fiterList.stream().forEach((a) -> System.out.println("a = " + a));

        ArrayList<String> strInteger = new ArrayList<>();
        strInteger.add("1");
        strInteger.add("2");
        strInteger.add("3");
        strInteger.add("4");
        strInteger.add("5");
        // 将list集合中的String类型元素 转换成Integer类型元素
        List<Integer> strToIntList = strInteger.stream().map(a -> Integer.parseInt(a)).collect(Collectors.toList());
        //strToIntList.stream().forEach((a) -> System.out.println("a = " + a));

        // 统计元素个数 与 list自带的size()方法一致 但  size()方法返回的为int类型
        int size = strInteger.size();
        // System.out.println("strInteger.size() = " + size);
        // stream().count()返回的为long类型 如果后续需要操作 则需转换数据类型
        long count = strInteger.stream().count();
        // System.out.println("strInteger.stream().count() = " + count);

        // 截取集合中前几条数据
        List<String> limitCollect = strInteger.stream().limit(3).collect(Collectors.toList());
        // limitCollect.stream().forEach((a) -> System.out.println("a = " + a));

        // 跳过前几条数据 保留后面的数据
        List<String> skipList = strInteger.stream().skip(3).collect(Collectors.toList());
        // skipList.stream().forEach((a) -> System.out.println("a = " + a));

        // 实现两个流数据的合并 前置位集合数据在前,后置位集合数据在后
        List<String> concatList = Stream.concat(strInteger.stream(), strList.stream()).collect(Collectors.toList());
        // concatList.stream().forEach((a) -> System.out.println("a = " + a));

        // 利用stream流 对实体集合进行操作
        ArrayList<PersonModel> personModels = new ArrayList<>();
        // 构建实体集合数据
        PersonModel personModel = new PersonModel("张三", "30");
        PersonModel personModel2 = new PersonModel("李四", "25");
        PersonModel personModel3 = new PersonModel("王五", "27");
        PersonModel personModel4 = new PersonModel("张三", "28");
        personModels.add(personModel);
        personModels.add(personModel2);
        personModels.add(personModel3);
        personModels.add(personModel4);

        // 实体类集合利用stream流进行遍历
        // personModels.stream().forEach((a) -> System.out.println("a = " + a.toString()));
        // 遍历数据
        // personModels.stream().forEach(System.out::println);

        // 利用fitler过滤数据 获取集合中符合条件的元素
        List<PersonModel> nameFilterList = personModels.stream().filter(a -> a.getName().equals("张三")).collect(Collectors.toList());
        // nameFilterList.stream().forEach(System.out::println);

        // 获取名为李四的信息 没有的话就返回一个新的学生对象 可自定义学生对象的属性列表
        PersonModel personModel1 = personModels.stream().filter(a -> a.getName().equals("九")).findAny().orElse(new PersonModel("暂无此学生", null));
        // System.out.println("personModel1 = " + personModel1);

        // 根据姓名 去除姓名重复的数据
         List<String> distinctfirstList = personModels.stream().map(PersonModel::getName).distinct().collect(Collectors.toList());
        // collect.stream().forEach(System.out::println);
        ArrayList<PersonModel> distinctSecondList = personModels.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<PersonModel>(
                        Comparator.comparing(PersonModel::getName))), ArrayList::new
        ));
        // collect.stream().forEach(System.out::println);

        // 根据集合中的元素排序 正序
        List<PersonModel> sortedAgeList = personModels.stream().sorted(Comparator.comparing(PersonModel::getAge)).collect(Collectors.toList());
        // sortedAgeList.stream().forEach(System.out::println);
        // 根据集合中的元素排序 倒序
        List<PersonModel> resortedAgeList = personModels.stream().sorted(Comparator.comparing(PersonModel::getAge).reversed()).collect(Collectors.toList());
        // resortedAgeList.stream().forEach(System.out::println);

        // 根据姓名进行分组 根据姓名分组会将条件作为key值放置在map集合中,而value的值则会是对应的实体对象并包含所有属性
        Map<String, List<PersonModel>> groupByNameList = personModels.stream().collect(Collectors.groupingBy(PersonModel::getName));
        /*Set<Map.Entry<String, List<PersonModel>>> entries = groupByNameList.entrySet();
        for (Map.Entry<String, List<PersonModel>> entry : entries) {
            System.out.println("entry.getKey()+\" === \"+entry.getValue() = " + entry.getKey() + " === " + entry.getValue());
        }*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值