標籤:種子 binary atm short mapped 一對一 order rcu compareto
https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/
構造Stream
// 1. Individual valuesStream stream = Stream.of("a", "b", "c");// 2. ArraysString [] strArray = new String[] {"a", "b", "c"};stream = Stream.of(strArray);stream = Arrays.stream(strArray);// 3. CollectionsList<String> list = Arrays.asList(strArray);stream = list.stream();
數值流的構造
//數值流有IntStream、DoubleStream、LongStream三種,目的是為了減少封箱拆箱的花費
IntStream.of(new int[]{1, 2, 3}).forEach(System.out::println);IntStream.range(1, 3).forEach(System.out::println);IntStream.rangeClosed(1, 3).forEach(System.out::println);
流轉化為其他資料結構
// 1. ArrayString[] strArray1 = stream.toArray(String[]::new);// 2. CollectionList<String> list1 = stream.collect(Collectors.toList());List<String> list2 = stream.collect(Collectors.toCollection(ArrayList::new));Set set1 = stream.collect(Collectors.toSet());Stack stack1 = stream.collect(Collectors.toCollection(Stack::new));// 3. StringString str = stream.collect(Collectors.joining()).toString(); //joining就是把流的每一個元素串連起來,可以傳入參數表示串連的時候中間的串連符號
流的操作分類
map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit
map/flatMap(映射)
//一對一的映射
List<String> output = wordList.stream().map(String::toUpperCase).collect(Collectors.toList());List<Integer> nums = Arrays.asList(1, 2, 3, 4);List<Integer> squareNums = nums.stream().map(n -> n * n).collect(Collectors.toList());
//一對多
Stream<List<Integer>> inputStream = Stream.of(
Arrays.asList(1), Arrays.asList(2, 3),Arrays.asList(4, 5, 6));
Stream<Integer> outputStream = inputStream.flatMap(childList -> childList.stream());
所謂flat,就是降低了這個流的維度,把每一個元素拿出來組成一個新的流,所謂的使得流扁平了
filter
List<String> output = reader.lines(). flatMap(line -> Stream.of(line.split(REGEXP))). filter(word -> word.length() > 0). collect(Collectors.toList());
//這裡我reader是BufferedReader,lines方法返回每一行的資料作為一個元素,注意Stream一次性的特點,消耗之後就不能在使用看來,在處理檔案相關內容的時候要格外的注意
forEach
stream.filter(p -> p.getGender() == Person.Sex.MALE)
.forEach(p -> System.out.println(p.getName()));
forEach 方法接收一個 Lambda 運算式,然後在 Stream 的每一個元素上執行該運算式。
forEach 是 terminal 操作,因此它執行後,Stream 的元素就被“消費”掉了,你無法對一個 Stream 進行兩次 terminal 運算
peek
對每個元素執行操作並返回一個新的 StreamStream.of("one", "two", "three", "four") .filter(e -> e.length() > 3) .peek(e -> System.out.println("Filtered value: " + e)) .map(String::toUpperCase) .peek(e -> System.out.println("Mapped value: " + e)) .collect(Collectors.toList());
findFirst
一個 termimal 兼 short-circuiting 操作,它總是返回 Stream 的第一個元素,或者空。傳回值類型:Optional
reduce
提供一個起始值(種子),然後依照運算規則(BinaryOperator),和前面 Stream 的第一個、第二個、第 n 個元素組合。從這個意義上說,字串拼接、數值的 sum、min、max、average 都是特殊的 reduceStream.reduce(0, (a, b) -> a+b)等價於sum()// 字串串連,concat = "ABCD"String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat); // 求最小值,minValue = -3.0double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min); // 求和,sumValue = 10, 有起始值int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);// 求和,sumValue = 10, 無起始值,這裡返回的是Optional類型sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();// 過濾,字串串連,concat = "ace"concat = Stream.of("a", "B", "c", "D", "e", "F"). filter(x -> x.compareTo("Z") > 0). reduce("", String::concat);
limit/skip
limit 返回 Stream 的前面 n 個元素;skip 則是扔掉前 n 個元素。limit 和 skip 對 sorted 後的運行次數無影響,就是說你不知道sorted的結果你limit沒有用,不知道從哪裡limit還是一樣得排序
sorted
List<Person> personList2 = persons.stream().limit(2).sorted((p1, p2) -> p1.getName().compareTo(p2.getName())).collect(Collectors.toList());沒有實現Comparable的要指定Comparable(Lumbda運算式)
java 8 Stream 代碼清單+API介紹