国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

jdk8特性例子 流 Streams

2019-11-08 20:02:47
字體:
來源:轉載
供稿:網友

java.util.Stream rePResents a sequence of elements on which one or more Operations can be performed. Stream operations are either intermediate or terminal. While terminal operations return a result of a certain type, intermediate operations return the stream itself so you can chain multiple method calls in a row. Streams are created on a source, e.g. a java.util.Collection like lists or sets (maps are not supported). Stream operations can either be executed sequential or parallel.

       stream 表示一個序列,在這個序列上可以執行一個或多個操作。流操作分為中間操作和最終操作兩類,最終操作返回一個特定類型的結果,中間結果返回流對象,這樣可以在同一行鏈式調用多個方法。創建stream要指定數據源,collection 例如list 、set(不支持map)。stream操作可以串行、也可以并行。

List<String> stringCollection = new ArrayList<>();stringCollection.add("ddd2");stringCollection.add("aaa2");stringCollection.add("bbb1");stringCollection.add("aaa1");stringCollection.add("bbb3");stringCollection.add("ccc");stringCollection.add("bbb2");stringCollection.add("ddd1");      stream 可以通過Collection.stream() or Collection.parallelStream()  創建。

Filter

   Filter accepts a predicate to filter all elements of the stream. This operation is intermediatewhich enables us to call another stream operation (forEach) on the result. ForEach accepts a consumer to be executed for each element in the filtered stream. ForEach is a terminal operation. It's void, so we cannot call another stream operation.

     Filter 接受一個謂詞過濾stream里的所有元素。filter是中間操作,其結果可以調用另一個操作。forEach 接受一個函數【函數式接口Consumer】,操作流對象里的每一個元素。forEach是最終操作。不能再調用另一個流操作。

     

		// aaa2 aaa1		stringCollection.stream().filter((s) -> s.startsWith("a")).forEach(System.out::println);

Sorted

Sorted is an intermediate operation which returns a sorted view of the stream. The elements are sorted in natural order unless you pass a custom Comparator.      sorted 是中間操作,返回stream排序后的視圖。流元素自然順序排序,除非指定一個Comparator。
		// "aaa1", "aaa2"		stringCollection.stream().sorted().filter((s) -> s.startsWith("a")).forEach(System.out::println);Keep in mind that sorted does only create a sorted view of the stream without manipulating the ordering of the backed collection. The ordering of stringCollection is untouched:       注意 sorted只是創建一個流的排序視圖,而不會修改原來集合的順序。原來對象stringCollection 不會被修改。
		// ddd2, aaa2, bbb1, aaa1, bbb3, ccc, bbb2, ddd1		System.out.println(stringCollection);

Map

The intermediate operation map converts each element into another object via the given function. The following example converts each string into an upper-cased string. But you can also use map to transform each object into another type. The generic type of the resulting stream depends on the generic type of the function you pass to map.

     map是一個中間操作,它把每個元素通過指定的方法轉換成另一個對象。下面的例子吧string轉換成大寫的string,也可以把對象轉換成其他類型。結果流的類型依賴傳入給map的函數的執行結果的類型。
		// "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1"		stringCollection.stream().map(String::toUpperCase).sorted((a, b) -> b.compareTo(a))				.forEach(System.out::println);

Match

Various matching operations can be used to check whether a certain predicate matches the stream. All of those operations are terminal and return a boolean result.

          提供各種match匹配操作,用于檢查指定的predicate是否匹配整個stream。所有的這些匹配操作是最終操作,返回boolean結果。
		boolean anyStartsWithA = stringCollection.stream().anyMatch((s) -> s.startsWith("a"));		System.out.println(anyStartsWithA); // true		boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));		System.out.println(allStartsWithA); // false		boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));		System.out.println(noneStartsWithZ); // true

Count

Count is a terminal operation returning the number of elements in the stream as a long.

     count是一個最終操作,返回stream中的元素個數,返回值是long類型。

		long startsWithB = stringCollection.stream().filter((s) -> s.startsWith("b")).count();		System.out.println(startsWithB); // 3

Reduce 簡化|規約 翻譯都不好

This terminal operation performs a reduction on the elements of the stream with the given function. The result is an Optional holding the reduced value.

 reduce操作是對流上的元素根據指定的函數執行reduction操作。結果是Optional,保存簡化后的結果。

		Optional<String> reduced = stringCollection.stream().sorted().reduce((s1, s2) -> s1 + "#" + s2);		// "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2"		reduced.ifPresent(System.out::println);ParallelStream

As mentioned above streams can be either sequential or parallel. Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrent on multiple threads.

    stream可以是串行也可以是并行的。串行stream單線程執行的,并行stream是多線程并行執行的。

public class ParallelStreamTest {	public static void main(String[] args) {		int max = 1000000;		List<String> values = new ArrayList<>(max);		for (int i = 0; i < max; i++) {			UUID uuid = UUID.randomUUID();			values.add(uuid.toString());		}		long t0 = System.nanoTime();		long count = values.stream().sorted().count();		System.out.println(count);		long t1 = System.nanoTime();		long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0);		System.out.println(String.format("sequential sort took: %d ms", millis));		long t2 = System.nanoTime();		long count2 = values.parallelStream().sorted().count();		System.out.println(count2);		long t3 = System.nanoTime();		long millis2 = TimeUnit.NANOSECONDS.toMillis(t3 - t2);		System.out.println(String.format("parallel sort took: %d ms", millis2));	}}結果大概差 50%。

1000000sequential sort took: 1271 ms1000000parallel sort took: 560 ms

     


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遵化市| 荣成市| 博白县| 镶黄旗| 泊头市| 绵阳市| 曲靖市| 玛纳斯县| 鸡西市| 青田县| 梁山县| 灵寿县| 荥阳市| 中卫市| 安达市| 昭觉县| 麦盖提县| 凤阳县| 卢氏县| 祥云县| 安康市| 库车县| 彭阳县| 大埔县| 峡江县| 通化市| 鄂托克前旗| 海林市| 涡阳县| 独山县| 顺昌县| 涟源市| 镇沅| 新邵县| 台中县| 阿克| 沙田区| 武威市| 哈密市| 周宁县| 桃江县|