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


 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);


Filtered elements...

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started