1. 概述

本文,我们将快速学习 Java 8 中新增的主要功能之一 —— Stream。

我们将解释什么是 stream,并通过简单的示例演示 Stream 的创建和基本操作。

2. Stream API

One of the major new features in Java 8 is the introduction of the stream functionality – java.util.stream – which contains classes for processing sequences of elements.

The central API class is the Stream. The following section will demonstrate how streams can be created using the existing data-provider sources.

2.1. Stream 的创建

Streams can be created from different element sources e.g. collection or array with the help of stream() and of() methods:

String[] arr = new String[]{"a", "b", "c"};
Stream<String> stream = Arrays.stream(arr);
stream = Stream.of("a", "b", "c");

A stream() default method is added to the Collection interface and allows creating a Stream using any collection as an element source*:*

Stream<String> stream = list.stream();

2.2. 多线程与 Stream

Stream API also simplifies multithreading by providing the parallelStream() method that runs operations over stream's elements in parallel mode.

The code below allows to run method doWork() in parallel for every element of the stream:

list.parallelStream().forEach(element -> doWork(element));

In the following section, we will introduce some of the basic Stream API operations.

3. Stream 操作

There are many useful operations that can be performed on a stream.

They are divided into intermediate operations (return Stream) and terminal operations (return a result of definite type). Intermediate operations allow chaining.

It's also worth noting that operations on streams don't change the source.

Here's a quick example:

long count = list.stream().distinct().count();

So, the distinct() method represents an intermediate operation, which creates a new stream of unique elements of the previous stream. And the count() method is a terminal operation*,* which returns stream's size.

3.1. Iterating - 迭代遍历

Stream API helps to substitute for, for-each, and while loops. It allows concentrating on operation's logic, but not on the iteration over the sequence of elements. For example:

for (String string : list) {
    if (string.contains("a")) {
        return true;
    }
}

This code can be changed just with one line of Java 8 code:

boolean isExist = list.stream().anyMatch(element -> element.contains("a"));

3.2. Filtering - 元素筛选过滤

The filter() method allows us to pick a stream of elements that satisfy a predicate.

For example, consider the following list:

ArrayList<String> list = new ArrayList<>();
list.add("One");
list.add("OneAndOnly");
list.add("Derek");
list.add("Change");
list.add("factory");
list.add("justBefore");
list.add("Italy");
list.add("Italy");
list.add("Thursday");
list.add("");
list.add("");

The following code creates a Stream of the List, finds all elements of this stream which contain char “d”, and creates a new stream containing only the filtered elements:

Stream<String> stream = list.stream().filter(element -> element.contains("d"));

3.3. Mapping - 元素映射

To convert elements of a Stream by applying a special function to them and to collect these new elements into a Stream, we can use the map() method:

List<String> uris = new ArrayList<>();
uris.add("C:\\My.txt");
Stream<Path> stream = uris.stream().map(uri -> Paths.get(uri));

So, the code above converts Stream to the Stream by applying a specific lambda expression to every element of the initial Stream.

If you have a stream where every element contains its own sequence of elements and you want to create a stream of these inner elements, you should use the flatMap() method:

List<Detail> details = new ArrayList<>();
details.add(new Detail());
Stream<String> stream
  = details.stream().flatMap(detail -> detail.getParts().stream());

In this example, we have a list of elements of type Detail. The Detail class contains a field PARTS, which is a List. With the help of the flatMap() method, every element from field PARTS will be extracted and added to the new resulting stream. After that, the initial Stream will be lost*.*

3.4. Matching - 查找匹配

Stream API gives a handy set of instruments to validate elements of a sequence according to some predicate. To do this, one of the following methods can be used: anyMatch(), allMatch(), noneMatch(). Their names are self-explanatory. Those are terminal operations that return a boolean:

boolean isValid = list.stream().anyMatch(element -> element.contains("h")); // true
boolean isValidOne = list.stream().allMatch(element -> element.contains("h")); // false
boolean isValidTwo = list.stream().noneMatch(element -> element.contains("h")); // false

For empty streams, the allMatch() method with any given predicate will return true:

Stream.empty().allMatch(Objects::nonNull); // true

This is a sensible default, as we can't find any element that doesn't satisfy the predicate.

Similarly, the anyMatch() method always returns false for empty streams:

Stream.empty().anyMatch(Objects::nonNull); // false

Again, this is reasonable, as we can't find an element satisfying this condition.

3.5. Reduction - 聚合求值

Stream API allows reducing a sequence of elements to some value according to a specified function with the help of the reduce() method of the type Stream. This method takes two parameters: first – start value, second – an accumulator function.

Imagine that you have a List and you want to have a sum of all these elements and some initial Integer (in this example 23). So, you can run the following code and result will be 26 (23 + 1 + 1 + 1).

List<Integer> integers = Arrays.asList(1, 1, 1);
Integer reduced = integers.stream().reduce(23, (a, b) -> a + b);

3.6. Collecting

The reduction can also be provided by the collect() method of type Stream. This operation is very handy in case of converting a stream to a Collection or a Map and representing a stream in the form of a single string*.* There is a utility class Collectors which provide a solution for almost all typical collecting operations. For some, not trivial tasks, a custom Collector can be created.

List<String> resultList 
  = list.stream().map(element -> element.toUpperCase()).collect(Collectors.toList());

This code uses the terminal collect() operation to reduce a Stream to the List.

4. 总结

In this article, we briefly touched upon Java streams — definitely one of the most interesting Java 8 features.

There are many more advanced examples of using Streams; the goal of this write-up was only to provide a quick and practical introduction to what you can start doing with the functionality and as a starting point for exploring and further learning.

The source code accompanying the article is available over on GitHub.


« 上一篇: Java 8 新特性