LongStream对象转化为List对象

场景

使用Random类获取伪随机数时,发现longs方法获得了LongStream对象,而我想将其转换为List对象,因为不熟悉流式编程所以特此记录。

语法与说明

 R collect(Supplier supplier,  ObjLongConsumer accumulator,  BiConsumer combiner)

对流对象做转换。

例子

将LongStream对象转换为List对象

        Random random = new Random();
        LongStream longs = random.longs(10, 0L, 3000L);
        ArrayList collect = longs.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        LongStream longs1 = random.longs(3, 3000L, 10000L);
        ArrayList collect1 = longs1.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        collect.addAll(collect1);
        System.out.println(collect);

结果:

[2500, 1400, 1289, 184, 179, 875, 800, 2580, 1608, 318, 3844, 3359, 8699]

Original: https://www.cnblogs.com/ceeSomething8/p/15972464.html
Author: cee_nil
Title: LongStream对象转化为List对象

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/712288/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球