Course Content
Stream API
Stream API
Restricting and Skipping Elements with the limit() and skip() Methods
The Java Stream API provides convenient methods for working with collections of data. Two useful methods, limit()
and skip()
, allow you to control how many elements are processed in a stream or skip a certain number of elements, which can be helpful for optimizing performance when dealing with large datasets.
What Are These Methods?
Main
package com.example; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Main { public static void main(String[] args) { List<String> products = Arrays.asList( "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12" ); // Limit processing to the first 10 items products.stream() .limit(10) .forEach(System.out::println); // Output: Item 1, Item 2, ..., Item 10 } }
In this example, you use limit(10)
to restrict the stream to the first 10 elements, ensuring that only those items are processed.
Main
package com.example; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Main { public static void main(String[] args) { List<String> products = Arrays.asList( "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12" ); // Skip the first 5 items and process the rest products.stream() .skip(5) // Skips the first 5 items .forEach(System.out::println); // Output: Item 6, Item 7, ..., Item 12 } }
Here, you use skip(5)
to ignore the first five items and start processing from the sixth item onward.
Real-World Example
Imagine you work at a factory with a list of 50 parts that need inspection. You only need to check the first 10 parts while another specialist handles the rest. In this case, you can use limit(10)
.
If the first 20 parts have already been inspected and you need to start from part 21, you can use skip(20)
.
Now, let's implement this scenario in code.
Practical Application
At a factory, you need to process parts. You have a list of 50 parts and need to skip the first 20, then process the next 10.
Main
package com.example; import java.util.UUID; import java.util.function.Supplier; import java.util.stream.Stream; public class Main { public static void main(String[] args) { // Supplier for generating parts Supplier<Part> partSupplier = () -> new Part( (int) (Math.random() * 1000), "Part-" + UUID.randomUUID() ); // Create a stream of 50 parts, skip the first 20, and process the next 10 Stream.generate(partSupplier) .limit(50) // Limit the stream to 50 parts .skip(20) // Skip the first 20 .limit(10) // Process the next 10 .forEach(System.out::println); // Print the parts } } class Part { private int id; private String name; Part(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "Part{id=" + id + ", name='" + name + "'}"; } }
In this code, you use a Supplier
to dynamically create Part
objects. The UUID
library helps generate unique identifiers for each part.
Using Stream.generate(partSupplier)
, which takes our Supplier
, you create a stream of parts that keeps generating elements until you limit it with limit(50)
.
Next, skip(20)
ensures the first 20 parts are ignored, while limit(10)
processes only the next 10 parts.
This approach allows efficient data generation and processing without needing to store the entire list in memory.
1. What does the limit(n)
method do in a stream?
2. Which interface is used to generate elements in a stream with Stream.generate()
?
3. Which method skips the first n
elements in a stream?
Thanks for your feedback!