Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Get a Range of Items from a Stream in Java

$
0
0

1. Overview

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.

2. Understanding Streams in 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.

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

To extract a range of items from a stream in Java, we have several approaches at our disposal. Let’s explore some of them.

3.1. Using the skip() and limit() Operations

The skip() and limit() operations are often combined to extract a range of items from a stream. Let’s see them in action:

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:

3, 4, 5, 6, 7

3.2. Using 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:

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, 4, 5, 6, 7

4. Conclusion

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.

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.

As usual, the source code for all of these examples is available over on GitHub.

       

Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>