Java8 Stream流中的 collect() 方法,遠(yuǎn)比你想象中的強(qiáng)大

Stream流 collect() 方法的使用介紹

//1.
<R> R collect(Supplier<R> supplier,
                  BiConsumer<R, ? super T> accumulator,
                  BiConsumer<R, R> combiner);
//2.
<R, A> R collect(Collector<? super T, A, R> collector);

Stream 流的注意事項:Stream不調(diào)用終止方法,中間的操作不會執(zhí)行。

但是,當(dāng)我們對 Stream 流中的數(shù)據(jù)操作完成之后,如果需要將流的結(jié)果進(jìn)行保存,方便我們接下來對結(jié)果的繼續(xù)操作,該怎么辦呢?

Stream 流提供了一個 collect() 方法,可以收集流中的數(shù)據(jù)到【集合】或者【數(shù)組】中去。

1.收集 Stream 流中的數(shù)據(jù)到集合中

//1.收集數(shù)據(jù)到list集合中
stream.collect(Collectors.toList())
//2.收集數(shù)據(jù)到set集合中
stream.collect(Collectors.toSet())
//3.收集數(shù)據(jù)到指定的集合中
Collectors.toCollection(Supplier<C> collectionFactory)
stream.collect(Collectors.joining())

示例如下:

/**
 * 收集Stream流中的數(shù)據(jù)到集合中
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 */
public class CollectDataToCollection{

    public static void main(String[] args) {
        //Stream 流
        Stream<String> stream = Stream.of("aaa", "bbb", "ccc", "bbb");
        //收集流中的數(shù)據(jù)到集合中
        //1.收集流中的數(shù)據(jù)到 list
        List<String> list = stream.collect(Collectors.toList());
        System.out.println(list);

        //2.收集流中的數(shù)據(jù)到 set
        Set<String> collect = stream.collect(Collectors.toSet());
        System.out.println(collect);

        //3.收集流中的數(shù)據(jù)(ArrayList)(不收集到list,set等集合中,而是)收集到指定的集合中
        ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));
        System.out.println(arrayList);

        //4.收集流中的數(shù)據(jù)到 HashSet
        HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet::new));
        System.out.println(hashSet);
    }
}

測試結(jié)果:

[aaa, bbb, ccc, bbb]
[aaa, ccc, bbb]
[aaa, bbb, ccc, bbb]
[aaa, ccc, bbb]

2.收集 Stream 流中的數(shù)據(jù)到數(shù)組中

//1.使用無參,收集到數(shù)組,返回值為 Object[](Object類型將不好操作)
Object[] toArray();
//2.使用有參,可以指定將數(shù)據(jù)收集到指定類型數(shù)組,方便后續(xù)對數(shù)組的操作
<A> A[] toArray(IntFunction<A[]> generator);

示例如下:

/**
 * 收集Stream流中的數(shù)據(jù)到數(shù)組中
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 */
public class CollectDataToArray{

    public static void main(String[] args) {
        //Stream 流
        Stream<String> stream = Stream.of("aaa", "bbb", "ccc", "bbb");

        //2.1 使用 toArray()無參
        Object[] objects = stream.toArray();
        for (Object o: objects) {//此處無法使用.length() 等方法
            System.out.println("data:"+o);
        }

        //2.2 使用有參返回指定類型數(shù)組
        //無參不好的一點(diǎn)就是返回的是 Object[] 類型,操作比較麻煩.想要拿到長度,Object是拿不到長度的
        String[] strings = stream.toArray(String[]::new);
        for(String str : strings){
            System.out.println("data:"+str + ",length:"+str.length());
        }
    }
}

測試結(jié)果:

data:aaa
data:bbb
data:ccc
data:bbb
-----------------
data:aaa,length:3
data:bbb,length:3
data:ccc,length:3
data:bbb,length:3

3.Stream流中數(shù)據(jù)聚合/分組/分區(qū)/拼接操作

除了 collect() 方法將數(shù)據(jù)收集到集合/數(shù)組中。對 Stream流 的收集還有其他的方法。比如說:聚合計算,分組,多級分組,分區(qū),拼接等。

附:Student實(shí)體類(接下來介紹,將根據(jù)Student類來進(jìn)行聚合、分組、分區(qū)、拼接介紹)

/**
 * TODO Student實(shí)體類
 *
 * @author liuzebiao
 */
public class Student {

    private String name;

    private int age;

    private int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

1.聚合操作

當(dāng)我們使用 Stream 流處理數(shù)據(jù)后,可以像數(shù)據(jù)庫的聚合函數(shù)一樣對某個字段進(jìn)行操作。比如獲取最大值,獲取最小值,求總和,求平均值,統(tǒng)計數(shù)量等操作。

//最大值
Collectors.maxBy();
//最小值
Collectors.minBy();
//總和
Collectors.summingInt();/Collectors.summingDouble();/Collectors.summingLong();
//平均值
Collectors.averagingInt();/Collectors.averagingDouble();/Collectors.averagingLong();
//總個數(shù)
Collectors.counting();

示例如下:

/**
 * Stream流數(shù)據(jù)--聚合操作
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 * @author liuzebiao
 */
public class CollectDataToArray{

    public static void main(String[] args) {
        Stream<Student> studentStream = Stream.of(
                new Student("趙麗穎", 58, 95),
                new Student("楊穎", 56, 88),
                new Student("迪麗熱巴", 56, 99),
                new Student("柳巖", 52, 77)
        );

        //聚合操作
        //獲取最大值(Stream流 max()方法亦可)
        //max()方法實(shí)現(xiàn)
        //Optional<Student> max = studentStream.max((s1, s2) -> s1.getScore() - s2.getScore());
        //(聚合)實(shí)現(xiàn)
        Optional<Student> max = studentStream.collect(Collectors.maxBy((s1, s2) -> s1.getScore() - s2.getScore()));
        System.out.println("最大值:"+max.get());

        //獲取最小值(Stream流 min()方法亦可)
        //min()方法實(shí)現(xiàn)
        //Optional<Student> min = studentStream.max((s1, s2) -> s2.getScore() - s1.getScore());
        //(聚合)實(shí)現(xiàn)
        Optional<Student> min = studentStream.collect(Collectors.minBy((s1, s2) -> s1.getScore() - s2.getScore()));
        System.out.println("最小值:"+min.get());

        //求總和(使用Stream流的map()和reduce()方法亦可求和)
        //map()和reduce()方法實(shí)現(xiàn)
        //Integer reduce = studentStream.map(s -> s.getAge()).reduce(0, Integer::sum);
        //(聚合)簡化前
        //Integer ageSum = studentStream.collect(Collectors.summingInt(s->s.getAge()));
        //(聚合)使用方法引用簡化
        Integer ageSum = studentStream.collect(Collectors.summingInt(Student::getAge));
        System.out.println("年齡總和:"+ageSum);

        //求平均值
        //(聚合)簡化前
        //Double avgScore = studentStream.collect(Collectors.averagingInt(s->s.getScore()));
        //(聚合)使用方法引用簡化
        Double avgScore = studentStream.collect(Collectors.averagingInt(Student::getScore));
        System.out.println("分?jǐn)?shù)平均值:"+avgScore);

        //統(tǒng)計數(shù)量(Stream流 count()方法亦可)
        //count()方法實(shí)現(xiàn)
        //long count = studentStream.count();
        //(聚合)統(tǒng)計數(shù)量
        Long count = studentStream.collect(Collectors.counting());
        System.out.println("數(shù)量為:"+count);
    }
}

測試結(jié)果:

最大值:Student{name='迪麗熱巴', age=56, score=99}
最小值:Student{name='柳巖', age=52, score=77}
年齡總和:222
分?jǐn)?shù)平均值:89.75
數(shù)量為:4

2.分組操作

當(dāng)我們使用 Stream 流處理數(shù)據(jù)后,可以根據(jù)某個屬性來將數(shù)據(jù)進(jìn)行分組。

//接收一個 Function 參數(shù)
groupingBy(Function<? super T, ? extends K> classifier)

示例如下:

/**
 * Stream流數(shù)據(jù)--分組操作
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 * @author liuzebiao
 */
public class CollectDataToArray{

    public static void main(String[] args) {
        Stream<Student> studentStream = Stream.of(
                new Student("趙麗穎", 52, 56),
                new Student("楊穎", 56, 88),
                new Student("迪麗熱巴", 56, 99),
                new Student("柳巖", 52, 53)
        );

        //1.按照具體年齡分組
        Map<Integer, List<Student>> map = studentStream.collect(Collectors.groupingBy((s -> s.getAge())));
        map.forEach((key,value)->{
            System.out.println(key + "---->"+value);
        });

        //2.按照分?jǐn)?shù)>=60 分為"及格"一組  <60 分為"不及格"一組
        Map<String, List<Student>> map = studentStream.collect(Collectors.groupingBy(s -> {
            if (s.getScore() >= 60) {
                return "及格";
            } else {
                return "不及格";
            }
        }));
        map.forEach((key,value)->{
            System.out.println(key + "---->"+value.get());
        });

        //3.按照年齡分組,規(guī)約求每組的最大值最小值(規(guī)約:reducing)
        Map<Integer, Optional<Student>> reducingMap = studentStream.collect(
                Collectors.groupingBy(Student::getAge, 
                        Collectors.reducing(
                                BinaryOperator.maxBy(
                                        Comparator.comparingInt(Student::getScore)
                                )
                        )
                )
        );
        reducingMap .forEach((key,value)->{
            System.out.println(key + "---->"+value);
        });
    }
}

測試結(jié)果:

52---->[Student{name='趙麗穎', age=52, score=56}, Student{name='柳巖', age=52, score=53}]
56---->[Student{name='楊穎', age=56, score=88}, Student{name='迪麗熱巴', age=56, score=99}]
-----------------------------------------------------------------------------------------------
不及格---->[Student{name='趙麗穎', age=52, score=56}, Student{name='柳巖', age=52, score=53}]
及格---->[Student{name='楊穎', age=56, score=88}, Student{name='迪麗熱巴', age=56, score=99}]
-----------------------------------------------------------------------------------------------
52---->Student{name='趙麗穎', age=52, score=95}
56---->Student{name='楊穎', age=56, score=88}

3.多級分組操作

當(dāng)我們使用 Stream 流處理數(shù)據(jù)后,可以根據(jù)某個屬性來將數(shù)據(jù)進(jìn)行分組。

//接收兩個參數(shù): 1.Function 參數(shù)  2.Collector多級分組
groupingBy(Function<? super T, ? extends K> classifier,Collector<? super T, A, D> downstream) 

示例如下:

/**
 * Stream流數(shù)據(jù)--多級分組操作
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 * @author liuzebiao
 * @Date 2020-1-10 13:37
 */
public class CollectDataToArray{

    public static void main(String[] args) {
        Stream<Student> studentStream = Stream.of(
                new Student("趙麗穎", 52, 95),
                new Student("楊穎", 56, 88),
                new Student("迪麗熱巴", 56, 55),
                new Student("柳巖", 52, 33)
        );

        //多級分組
        //1.先根據(jù)年齡分組,然后再根據(jù)成績分組
        //分析:第一個Collectors.groupingBy() 使用的是(年齡+成績)兩個維度分組,所以使用兩個參數(shù) groupingBy()方法
        //    第二個Collectors.groupingBy() 就是用成績分組,使用一個參數(shù) groupingBy() 方法
        Map<Integer, Map<Integer, Map<String, List<Student>>>> map = studentStream.collect(Collectors.groupingBy(str -> str.getAge(), Collectors.groupingBy(str -> str.getScore(), Collectors.groupingBy((student) -> {
            if (student.getScore() >= 60) {
                return "及格";
            } else {
                return "不及格";
            }
        }))));

        map.forEach((key,value)->{
            System.out.println("年齡:" + key);
            value.forEach((k2,v2)->{
                System.out.println("\t" + v2);
            });
        });
    }
}

測試結(jié)果:

年齡:52
 {不及格=[Student{name='柳巖', age=52, score=33}]}
 {及格=[Student{name='趙麗穎', age=52, score=95}]}
年齡:56
 {不及格=[Student{name='迪麗熱巴', age=56, score=55}]}
 {及格=[Student{name='楊穎', age=56, score=88}]}

4.分區(qū)操作

我們在前面學(xué)習(xí)了 Stream流中數(shù)據(jù)的分組操作,我們可以根據(jù)屬性完成對數(shù)據(jù)的分組。接下來我們介紹分區(qū)操作,我們通過使用 Collectors.partitioningBy() ,根據(jù)返回值是否為 true,把集合分為兩個列表,一個 true 列表,一個 false 列表。

分組和分區(qū)的區(qū)別就在: 分組可以有多個組。分區(qū)只會有兩個區(qū)( true 和 false)

//1.一個參數(shù)
partitioningBy(Predicate<? super T> predicate)

//2.兩個參數(shù)(多級分區(qū))
partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)

示例如下:

/**
 * Stream流數(shù)據(jù)--多級分組操作
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 * @author liuzebiao
 */
public class CollectDataToArray{

    public static void main(String[] args) {
        Stream<Student> studentStream = Stream.of(
                new Student("趙麗穎", 52, 95),
                new Student("楊穎", 56, 88),
                new Student("迪麗熱巴", 56, 55),
                new Student("柳巖", 52, 33)
        );

        //分區(qū)操作
        Map<Boolean, List<Student>> partitionMap = studentStream.collect(Collectors.partitioningBy(s -> s.getScore() > 60));
        partitionMap.forEach((key,value)->{
            System.out.println(key + "---->" + value);
        });
    }
}

測試結(jié)果:

false---->[Student{name='迪麗熱巴', age=56, score=55}, Student{name='柳巖', age=52, score=33}]
true---->[Student{name='趙麗穎', age=52, score=95}, Student{name='楊穎', age=56, score=88}]

5.拼接操作

Collectors.joining() 會根據(jù)指定的連接符,將所有元素連接成一個字符串。

//無參數(shù)--等價于 joining("");
joining()
//一個參數(shù)
joining(CharSequence delimiter)
//三個參數(shù)(前綴+后綴)
joining(CharSequence delimiter, CharSequence prefix,CharSequence suffix)

示例如下:

/**
 * Stream流數(shù)據(jù)--多級分組操作
 * 備注:切記Stream流只能被消費(fèi)一次,流就失效了
 * 如下只是示例代碼
 * @author liuzebiao
 */
public class CollectDataToArray{

    public static void main(String[] args) {
        Stream<Student> studentStream = Stream.of(
                new Student("趙麗穎", 52, 95),
                new Student("楊穎", 56, 88),
                new Student("迪麗熱巴", 56, 55),
                new Student("柳巖", 52, 33)
        );

        //拼接操作
        //無參:join()
        String joinStr1 = studentStream.map(s -> s.getName()).collect(Collectors.joining());
        System.out.println(joinStr1);
        //一個參數(shù):joining(CharSequence delimiter)
        String joinStr2 = studentStream.map(s -> s.getName()).collect(Collectors.joining(","));
        System.out.println(joinStr2);
        //三個參數(shù):joining(CharSequence delimiter, CharSequence prefix,CharSequence suffix)
        String joinStr3 = studentStream.map(s -> s.getName()).collect(Collectors.joining("—","^_^",">_<"));
        System.out.println(joinStr3);
    }
}

測試結(jié)果:

趙麗穎楊穎迪麗熱巴柳巖
趙麗穎,楊穎,迪麗熱巴,柳巖
^_^趙麗穎—楊穎—迪麗熱巴—柳巖>_<
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容