Java基础之Stream流

本文详细介绍了Java中的Stream API,包括从Collection、Map和数组生成流的方法。此外,还探讨了流的中间操作,如filter、limit、skip和concat等,以及终结操作,如forEach、count和collect。示例代码展示了如何使用这些操作筛选、截取、合并和统计流中的元素,并将流收集到集合中。通过这些操作,可以更高效地处理数据集合。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

Stream流

  • Collection体系集合:可以使用默认方法stream()生成流,
  • Map体系集合:先把Map转成Set集合,间接生成流。
  • 数组:通过Arrays工具类的stream方法
  • 同种数据类型的,通过Stream接口的静态方法of 生成流
        //Collection体系的集合可以使用默认方法stream()生成流
        List<String> list = new ArrayList<String>();
        Stream<String> listStream = list.stream();

        Set<String> set = new HashSet<String>();
        Stream<String> setStream = set.stream();

        //Map体系的集合间接的生成流
        Map<String,Integer> map = new HashMap<String, Integer>();
        Stream<String> keyStream = map.keySet().stream();
        Stream<Integer> valueStream = map.values().stream();
        Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();

        //数组可以通过Arrays中的静态方法stream生成流
        String[] strArray = {"hello","world","java"};
        Stream<String> strArrayStream = Arrays.stream(strArray);
      
      	//同种数据类型的多个数据可以通过Stream接口的静态方法of(T... values)生成流
        Stream<String> strArrayStream2 = Stream.of("hello","world", "java");
        
        Stream<Integer> intStream = Stream.of(10, 20, 30);

流的中间操作方法

方法名说明
Stream filter(Predicate predicate)用于对流中的数据进行过滤
Stream limit(long maxSize)返回此流中的元素组成的流,截取前指定参数个数的数据
Stream skip(long n)跳过指定参数个数的数据,返回由该流的剩余元素组成的流
static Stream concat(Stream a, Stream b)合并a和b两个流为一个流
Stream distinct()返回由该流的不同元素(根据Object.equals(Object) )组成的流
     ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("aaa");
        arrayList.add("bbb");
        arrayList.add("ccc");
        arrayList.add("abc");
        arrayList.add("acd");
        arrayList.add("abd");
        //筛选首字母为a的元素,
        arrayList.stream().filter((s)->s.startsWith("a")).forEach(System.out::println);
        System.out.println("=============");
        //返回前3个
        arrayList.stream().limit(3).forEach(System.out::println);
        System.out.println("=============");
        //跳过前3个
        arrayList.stream().skip(3).forEach(System.out::println);
        System.out.println("=============");

        //创建第二个集合
        ArrayList<String> arrayList1 = new ArrayList<>();
        arrayList.add("aaa");
        arrayList.add("bbb");
        arrayList.add("ccc");

        //两个流合并
        Stream<String> concat = Stream.concat(arrayList.stream(), arrayList1.stream());
        
        System.out.println("===============");
        //去重
        concat.distinct().forEach(System.out::println);
       

流的终结操作方法

方法名说明
void forEach(Consumer action)对此流的每个元素执行操作
long count()返回此流中的元素数
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("aaa");
        arrayList.add("bbb");
        arrayList.add("ccc");
        arrayList.add("abc");
        arrayList.add("acd");
        arrayList.add("abd");

        //forEach对每个元素执行操作
        arrayList.stream().forEach(s -> System.out.println(s));
        //统计元素数
        long a = arrayList.stream().count();
        System.out.println(a);

Stream收集操作
把流中的数据收集到集合中

方法

方法名说明
R collect(Collector collector)把结果收集到集合中

方法名说明
public static Collector toList()把元素收集到List集合中
public static Collector toSet()把元素收集到Set集合中
public static Collector toMap(Function keyMapper,Function valueMapper)把元素收集到Map集合中
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("aaa");
        arrayList.add("bbb");
        arrayList.add("ccc");
        arrayList.add("abc");
        arrayList.add("acd");
        arrayList.add("abd");

//截取前四个的流收集到集合中。
        List<String> collect = arrayList.stream().limit(4).collect(Collectors.toList());
        //打印集合
        System.out.println(collect);

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值