LongStream filter() method in Java

Java 8ProgrammingObject Oriented Programming

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
raja
Published on 26-Mar-2019 15:12:15
Advertisements