Description: Returns a stream consisting of the elements of this stream that match the given predicate.
// Find users whose first names are "John"
$stream->filter(function(User $user) {
return $user->getFirstName() === 'John';
});
Description: Returns the first element of matched items, or null if the stream is empty.
// Find the first user whose age is 28
$user = $stream->first(function(User $user) {
return $user->getAge() === 28;
});
Description: Returns the last element of matched items, or null if the stream is empty.
// Find the last user whose age is 28
$user = $stream->last(function(User $user) {
return $user->getAge() === 28;
});