1. Overview
1.概述
Introduced in Java 8, the Stream API has revolutionized how developers work with object collections. Streams provide powerful operations to process and manipulate data concisely and declaratively. One common task when working with streams is to extract a range of items from the stream based on certain criteria or indices. In this blog post, we’ll explore how to efficiently retrieve a range of items from a stream in Java.
在 Java 8 中引入的 Stream API 彻底改变了开发人员处理对象集合的方式。流提供了强大的操作,可简化和声明性地处理和操作数据。在使用流时,一个常见的任务是根据特定条件或索引从流中提取一系列项目。在本博文中,我们将探讨如何从 Java 流中高效地检索一系列项目。
2. Understanding Streams in Java
2.了解 Java 中的流
Before extracting a range of items from a stream, let’s have a quick overview of streams in Java. Streams represent elements that can be processed in a functional style. They enable developers to perform operations such as filtering, mapping, sorting, and reducing data collection.
在从数据流中提取一系列项目之前,让我们先快速了解一下 Java 中的数据流。流代表了可以用函数式风格处理的元素。它们使开发人员能够执行过滤、映射、排序和减少数据收集等操作。
Streams are composed of three parts:
溪流由三部分组成:
- Source: The source of elements, such as a collection, an array, or an I/O channel
- Intermediate Operations: Operations that transform the stream into another stream, such as filtering or mapping
- Terminal Operations: Operations that produce a result or a side-effect, such as forEach or collect
3. Getting a Range of Items from a Stream
3.从数据流中获取一系列项目
To extract a range of items from a stream in Java, we have several approaches at our disposal. Let’s explore some of them.
要从 Java 数据流中提取一系列项目,我们有多种方法可供选择。让我们来探讨其中一些。
3.1. Using the skip() and limit() Operations
3.1.使用 skip() 和 limit() 操作
The skip() and limit() operations are often combined to extract a range of items from a stream. Let’s see them in action:
skip()和limit()操作通常结合使用,以从数据流中提取一定范围的项目:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> range = numbers.stream()
.skip(2)
.limit(5)
.collect(Collectors.toList());
In this example, we skip the first two elements using the skip() operation and then limit the stream to the next five elements using the limit() operation. Therefore, our result is:
在这个示例中,我们使用 skip() 操作跳过头两个元素,然后使用 limit() 操作将数据流限制为接下来的五个元素。因此,我们的结果是
3, 4, 5, 6, 7
3.2. Using Collectors.collectingAndThen()
3.2.使用 Collectors.collectingAndThen()
We can use Collectors.collectingAndThen() along with Collectors.toList() to collect the elements into a list and then manipulate the list as needed. Let’s see an example:
我们可以使用 Collectors.collectingAndThen() 和 Collectors.toList() 将元素收集到列表中,然后根据需要对列表进行操作:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> range = numbers.stream()
.filter(n -> n >= 3 && n <= 7)
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
In this example, we filter the stream to include only elements between 3 and 7, and then we collect them into an unmodifiable list using Collectors.collectingAndThen(). Therefore, our result is:
在本例中,我们过滤了数据流,使其只包含 3 到 7 之间的元素,然后使用 Collectors.collectingAndThen() 将它们收集到一个不可修改的列表中。因此,我们的结果是
3, 4, 5, 6, 7
4. Conclusion
4.结论
The Stream API in Java provides powerful capabilities for processing and manipulating data. Extracting a range of items from a stream allows developers to efficiently work with large datasets and perform targeted operations on subsets of the data.
Java中的流API为处理和操作数据提供了强大的功能。从流中提取一系列项目可让开发人员高效地处理大型数据集,并对数据子集执行有针对性的操作。
By leveraging operations like skip(), limit(), and collectingAndThen(), developers can easily extract the desired range of elements from streams or collections. Understanding these techniques equips Java developers with the tools to write cleaner, more concise code that efficiently handles data processing tasks.
通过利用 skip()、limit() 和 collectingAndThen()等操作,开发人员可以轻松地从流或集合中提取所需的元素范围。了解了这些技术,Java 开发人员就掌握了编写更简洁的代码的工具,从而可以高效地处理数据处理任务。
As usual, the source code for all of these examples is available over on GitHub.
与往常一样,所有这些示例的源代码均可在 GitHub 上获取。