CV地址:https://blog.csdn.net/liudongdong0909/article/details/77429875
篩選與切片
| 方法 | 描述 |
|---|---|
| filter(Predicate p) | 接收 Lambda , 從流中排除某些元素。 |
| distinct() | 篩選,通過流所生成元素的 hashCode() 和 equals() 去 除重復元素 |
| limit(long maxSize) | 截斷流,使其元素不超過給定數量 |
| skip(long n) | 跳過元素,返回一個扔掉了前 n 個元素的流。若流中元素 不足 n 個,則返回一個空流。與 limit(n) 互補 |
映射
| 方法 | 描述 |
|---|---|
| map(Function f) | 接收一個函數作為參數,該函數會被應用到每個元 素上,並將其映射成一個新的元素。 |
| mapToDouble(ToDoubleFunction f) | 接收一個函數作為參數,該函數會被應用到每個元 素上,產生一個新的 DoubleStream。 |
| mapToInt(ToIntFunction f) | 接收一個函數作為參數,該函數會被應用到每個元 素上,產生一個新的 IntStream。 |
| mapToLong(ToLongFunction f) | 接收一個函數作為參數,該函數會被應用到每個元 素上,產生一個新的 LongStream。 |
| flatMap(Function f) | 接收一個函數作為參數,將流中的每個值都換成另一個流,然后把所有流連接成一個流 |
排序
| 方法 | 描述 |
|---|---|
| sorted() | 產生一個新流,其中按自然順序排序 |
| sorted(Comparator comp) | 產生一個新流,其中按比較器順序排序 |
查找與匹配
| 方法 | 描述 |
|---|---|
| allMatch(Predicate p) | 檢查是否匹配所有元素 |
| anyMatch(Predicate p) | 檢查是否至少匹配一個元素 |
| noneMatch(Predicate p) | 檢查是否沒有匹配所有元素 |
| findFirst() | 返回第一個元素 |
| findAny() | 返回當前流中的任意元素 |
| count() | 返回流中元素總數 |
| max(Comparator c) | 返回流中最大值 |
| min(Comparator c) | 返回流中最小值 |
| forEach(Consumer c) | 內部迭代(使用 Collection 接口需要用戶去做迭 代,稱為外部迭代。相反,Stream API 使用內部 迭代——它幫你把迭代做了) |
歸約
| 方法 | 描述 |
|---|---|
| reduce(T iden, BinaryOperator b) | 可以將流中元素反復結合起來,得到一個值。 返回 T |
| reduce(BinaryOperator b) | 可以將流中元素反復結合起來,得到一個值。 返回 Optional< T> |
收集
| 方法 | 描述 |
|---|---|
| collect(Collector c) | 將流轉換為其他形式。接收一個 Collector接口的 實現,用於給Stream中元素做匯總的方法 |
Collector 接口中方法的實現決定了如何對流執行收集操作(如收 集到 List、Set、Map)。但是 Collectors 實用類 供了很多靜態 方法,可以方便地創建常見收集器實例,具體方法與實例如下表:
| 方法 | 返回類型 | 作用 |
|---|---|---|
| toList | List<T> | 把流中元素收集到List |
| List<Employee> emps= list.stream().collect(Collectors.toList()); | ||
| toSet | Set<T> | 把流中元素收集到Set |
| Set<Employee> emps= list.stream().collect(Collectors.toSet()); | ||
| toCollection | Collection<T> | 把流中元素收集到創建的集合 |
| Collection<Employee>emps=list.stream().collect(Collectors.toCollection(ArrayList::new)); | ||
| counting | Long | 計算流中元素的個數 |
| long count = list.stream().collect(Collectors.counting()); | ||
| summingInt | Integer | 對流中元素的整數屬性求和 |
| inttotal=list.stream().collect(Collectors.summingInt(Employee::getSalary)); | ||
| averagingInt | Double | 計算流中元素Integer屬性的平均 值 |
| doubleavg= list.stream().collect(Collectors.averagingInt(Employee::getSalary)); | ||
| summarizingInt | IntSummaryStatistics | 收集流中Integer屬性的統計值。 如:平均值 |
| IntSummaryStatisticsiss= list.stream().collect(Collectors.summarizingInt(Employee::getSalary)); | ||
| joining | String | 連接流中每個字符串 |
| String str= list.stream().map(Employee::getName).collect(Collectors.joining()); | ||
| maxBy | Optional<T> | 根據比較器選擇最大值 |
| Optional<Emp>max= list.stream().collect(Collectors.maxBy(comparingInt(Employee::getSalary))); | ||
| minBy | Optional<T> | 根據比較器選擇最小值 |
| Optional<Emp> min = list.stream().collect(Collectors.minBy(comparingInt(Employee::getSalary))); | ||
| reducing | 歸約產生的類型 | 從一個作為累加器的初始值 開始,利用BinaryOperator與 流中元素逐個結合,從而歸 約成單個值 |
| inttotal=list.stream().collect(Collectors.reducing(0, Employee::getSalar, Integer::sum)); | ||
| collectingAndThen | 轉換函數返回的類型 | 包裹另一個收集器,對其結 果轉換函數 |
| inthow= list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), List::size)); | ||
| groupingBy | Map<K, List<T>> | 根據某屬性值對流分組,屬 性為K,結果為V |
| Map<Emp.Status, List<Emp>> map= list.stream() .collect(Collectors.groupingBy(Employee::getStatus)); | ||
| partitioningBy | Map<Boolean, List<T>> | 根據true或false進行分區 |
| Map<Boolean,List<Emp>>vd= list.stream().collect(Collectors.partitioningBy(Employee::getManage)); | ||
