當前位置: 妍妍網 > 碼農

你還在用for迴圈遍歷list嗎?

2024-02-06碼農

來源:blog.csdn.net/chaitoudaren/article/details/105122681

# 簡介

Java 8 API添加了一個新的抽象稱為流Stream,可以讓你以一種聲明的方式處理數據。

Stream 使用一種類似用 SQL 語句從資料庫查詢數據的直觀方式來提供一種對 Java 集合運算和表達的高階抽象。

這種風格將要處理的元素集合看作一種流, 流在管道中傳輸, 並且可以在管道的節點上進行處理, 比如篩選, 排序,聚合等。

熟悉Linux的同學對這種風格一定不陌生,因為它跟Linux的|管道符的思想如出一轍。上面這段話參照自runoob.com,但是其教學程式碼都是基於String列表進行演示,考慮到實際情況百分之80的時候都是對PO、VO進行處理,因此以下透過一個PO進行講解。

對比起for迴圈操作list,最大的弊端就是程式碼太長太亂了,如果涉及3-4張表的操作,也就是涉及多個PO操作,那個括弧簡直就是俄羅斯套娃,寫到最後真的自己都不知道在寫什麽。

#

+--------------------+ +------+ +------+ +---+ +-------+| stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|+--------------------+ +------+ +------+ +---+ +-------+

PO程式碼

public classUserPo{private String name;privateDouble score;// 省略建構函式及getter、setter}

以下操作均以UserPo進行講解

filter

filter:過濾,就是過濾器,符合條件的透過,不符合條件的過濾掉

// 篩選出成績不為空的學生人數count = list.stream().filter(p -> null != p.getScore()).count();

map

map:對映,他將原集合對映成為新的集合,在VO、PO處理的過程中較常見。在本例子中,原集合就是PO集合,新集合可以自訂對映為成績集合,同時也可以對新集合進行相關操作。

// 取出所有學生的成績List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());// 將學生姓名集合串成字串,用逗號分隔String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));

sorted

sorted:排序,可以根據指定的欄位進行排序

// 按學生成績逆序排序 正序則不需要加.reversed()filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());

forEach

forEach:這個應該是最常用的,也就是為每一個元素進行自訂操作

除了forEach操作會改變原集合的數據,其他的操作均不會改變原集合,這點務必引起註意

// 學生成績太差了,及格率太低,給每個學生加10分,放個水// forEachfilterList.stream().forEach(p -> p.setScore(p.getScore() + 10));

collect

collect:聚合,可以用於GroudBy按指定欄位分類,也可以用於返回列表或者拼湊字串

// 按成績進行歸集Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore()).collect(Collectors.groupingBy(UserPo::getScore));for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) { System.out.println("成績:" + entry.getKey() + " 人數:" + entry.getValue().size());}// 返回listList<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());// 返回string用逗號分隔String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));

statistics

statistics:統計,可以統計中位數,平均值,最大最小值

DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics();System.out.println("列表中最大的數 : " + statistics.getMax());System.out.println("列表中最小的數 : " + statistics.getMin());System.out.println("所有數之和 : " + statistics.getSum());System.out.println("平均數 : " + statistics.getAverage());

parallelStream

parallelStream:並列流,可以利用多執行緒進行流的操作,提升效率。但是其不具備執行緒傳播性,因此使用時需要充分評估是否需要用並列流操作

// 並列流count = list.parallelStream().filter(p -> null != p.getScore()).count();

# 完整程式碼

public classUserPo{ private String name; privateDouble score; public UserPo(String name, Double score) { this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } publicDouble getScore() { return score; } public void setScore(Double score) { this.score = score; } @Overridepublic String toString() { return"UserPo{" + "name='" + name + '\'' + ", score=" + score + '}'; }}

public classStreamTest{// +--------------------+ +------+ +------+ +---+ +-------+// | stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|// +--------------------+ +------+ +------+ +---+ +-------+publicstatic void main(String args[]){List<UserPo> list = new ArrayList<>();list.add(new UserPo("小一", 10.d));list.add(new UserPo("小五", 50.d));list.add(new UserPo("小六", 60.d));list.add(new UserPo("小6", 60.d));list.add(new UserPo("小空", null));list.add(new UserPo("小九"90.d)); long count = 0;List<UserPo> filterList = null;// filter 過濾器的使用// 篩選出成績不為空的學生人數 count = list.stream().filter(p -> null != p.getScore()).count(); System.out.println("參加考試的學生人數:" + count);// collect// 篩選出成績不為空的學生集合 filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList()); System.out.println("參加考試的學生資訊:"); filterList.stream().forEach(System.out::println);// map 將集合對映為另外一個集合// 取出所有學生的成績List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList()); System.out.println("所有學生的成績集合:" + scoreList);// 將學生姓名集合串成字串,用逗號分隔 String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(",")); System.out.println("所有學生的姓名字串:" + nameString);// sorted排序// 按學生成績逆序排序 正序則不需要加.reversed() filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList()); System.out.println("所有學生的成績集合,逆序排序:"); filterList.stream().forEach(System.out::println); System.out.println("按學生成績歸集:"); Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore()) .collect(Collectors.groupingBy(UserPo::getScore));for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) { System.out.println("成績:" + entry.getKey() + " 人數:" + entry.getValue().size()); }// forEach filterList.stream().forEach(p -> p.setScore(p.getScore() + 10)); System.out.println("及格人數太少,給每個人加10分"); filterList.stream().forEach(System.out::println);// count count = filterList.stream().filter(p -> p.getScore() >= 60).count(); System.out.println("最後及格人數" + count); DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics(); System.out.println("列表中最大的數 : " + statistics.getMax()); System.out.println("列表中最小的數 : " + statistics.getMin()); System.out.println("所有數之和 : " + statistics.getSum()); System.out.println("平均數 : " + statistics.getAverage());// 並列流 使用 count = list.parallelStream().filter(p -> null != p.getScore()).count(); System.out.println("並列流處理參加考試的學生人數:" + count); }}

熱門推薦