Stream 常用操作
allMatch
使用給定的 Predicate 檢查 Stream 中的所有元素,全部都通過檢測則返回 true,否則 false 。
-
System.out.println(Stream.of(1,2,3).allMatch(n -> n >= 1));
-
System.out.println(Stream.of(1,2,3).allMatch(n -> n >= 3));
-
System.out.println(Stream.of(1,2,3).allMatch(n -> n >= 4));
anyMatch
使用給定的 Predicate 檢查 Stream 中的所有元素,至少有一個通過檢測則返回 true,否則 false 。
collect
collect 操作使用給定的 Collector 做 reduce 操作。
數組元素連接
System.out.println(Stream.of("A", "B", "C").collect(Collectors.joining(",")));
轉成 List
List<String> asList = Stream.of("A", "B", "C").collect(Collectors.toList());
根據城市分組
Map<String, List<Person>> peopleByCity = personStream.collect(Collectors.groupingBy(Person::getCity));
** 根據州和城市分組**
-
Map<String, Map<String, List<Person>>> peopleByStateAndCity
-
= personStream.collect(Collectors.groupingBy(Person::getState,
-
Collectors.groupingBy(Person::getCity)));
count
返回 Stream 中的元素總數。
System.out.println(Stream.of(1,2,3).count());
distinct
返回唯一的元素列表,類似於 數據庫 sql 中的 distinct 關鍵字。 比較時通過 equals 方法來判定是否相同。
System.out.println(Stream.of(1,2,3,3).distinct().map(n -> n + "").collect(Collectors.joining(",")));
filter
使用給定的 Predicate 的篩選 Stream 元素,符合條件的留下並組成一個新的 Stream 。
System.out.println(Stream.of(1,2,3).filter(n -> n > 1).map(n -> n + "").collect(Collectors.joining(",")));
findAny
返回任何一個不確定的元素,通過 Optional 來包裝。如果在一個固定不變的組合中,返回第一個元素。
System.out.println(Stream.of(1,2,3).findAny().get());
findFirst
返回第一個元素。
System.out.println(Stream.of(1,2,3).findFirst().get());
flatMap
適用於如果Stream中的元素還是集合,能將集合中的元素組成一個平面的集合。簡單來下面的例子,Stream 是二維的,因爲 Stream 的元素還是數組,經過flag處理後,變成一維的了,所有元素位於一個Stream 下了。
-
System.out.println(
-
Stream.of(new Integer[]{1,2,3}, new Integer[]{4,5,6}, new Integer[]{7,8,9,0})
-
.flatMap(a -> Arrays.stream(a))
-
.map(n -> n + "").collect(Collectors.joining(",")));
-
// 輸出:1,2,3,4,5,6,7,8,9,0
forEach
逐個元素執行 Consumer 操作。
Stream.of(1,2,3).forEach(n -> System.out.print(n + ","));
limit
取出指定個數的元素組成新的 Stream .
System.out.println(Stream.of(1,2,3).limit(2).map(n -> n + "").collect(Collectors.joining(",")));
map
map 方法的作用是依次對 Stream 中的元素進行指定的函數操作,並將按順序將函數操作的返回值組合到一個新的 Stream 中。
下面例子將每個元素的值 +1
-
System.out.println(Stream.of(1,2,3).map(n -> n + 1).map(String::valueOf).collect(Collectors.joining(",")));
-
// 輸出 2,3,4
max
max 通過給定的比較器,將最大的元素取出來,返回 Optional
System.out.println(Stream.of(1,2,3).max((a, b) -> a - b).get());
min
min 通過給定的比較器,將最小的元素取出來,返回 Optional
System.out.println(Stream.of(1,2,3).min((a, b) -> a - b).get());
noneMatch
noneMatch 於 allMatch, anyMatch 類似,使用給定的 Predicate 檢查 Stream 中的所有元素,全部不通過檢測則返回 true,否則 false 。
-
System.out.println(Stream.of(1,2,3).noneMatch(n -> n > 1));
-
System.out.println(Stream.of(1,2,3).noneMatch(n -> n > 3 || n < 1));
reduce
reduce 的函數操作爲二元操作符,一個爲前面操作的結果,一個爲當前元素,reduce 會逐個對 Stream 中的元素執行指定的操作,並返回最終的結果。
如求和
-
System.out.println(Stream.of(1,2,3).reduce(0, (a, b) -> a + b));
-
或者
-
System.out.println(Stream.of(1,2,3).reduce((a, b) -> a + b).get());
skip
忽略給定個數的元素,返回剩下的元素組成 Stream 。
System.out.println(Stream.of(1,2,3).skip(1).map(n -> n + "").collect(Collectors.joining(",")));
sorted
通過給定的比較器排序,將排序後的元素的 Stream 返回。
-
System.out.println(Stream.of(1,2,3).sorted().map(n -> n + "").collect(Collectors.joining(",")));
-
System.out.println(Stream.of(1,2,3).sorted((a, b) -> b - a).map(n