Groovy already has a lot of extra methods for Stream
classes.
With Groovy 5 the getAt
method is added as a terminal operation.
You can now use the []
syntax to get an item from a Stream
from a specific position.
The argument is the index of the item or a range with the start and end index of the items to get.
In the following example code the getAt
method and []
operator are used:
import java.util.stream.Stream def list = ['Groovy', '5', 'is', 'awesome'] // Use index operator. // This is a terminal operation for the stream. assert list.stream()[0] == 'Groovy' assert list.stream().getAt(0) == 'Groovy' // Using a range as argument is possible. assert list.stream()[2..3] == ['is', 'awesome'] assert list.stream().getAt(2..3) == ['is', 'awesome'] assert list.stream()[0..<2] == ['Groovy', '5']
Written with Groovy 5.0.0.