LongStream Filter Method in Java



The filter() method in the LongStream class in Java returns a stream consisting of the elements of this stream that match the given predicate.

The syntax is as follows

LongStream filter(LongPredicate predicate)

Here, the parameter predicate is a stateless predicate to apply to each element to determine if it should be included. The LongPredicate represents a predicate (boolean-valued function) of one long-valued argument

To use the LongStream class in Java, import the following package

import java.util.stream.LongStream;

The following is an example to implement LongStream filter() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
LongStream longStream = LongStream.of(10L, 12L, 18L, 20L, 25L, 30L);
System.out.println("Filtered elements...");
longStream.filter(a -> a > 20L).forEach(System.out::println);
}
}

Output

Filtered elements...
25
30
Updated on: 2019-07-30T22:30:25+05:30

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements