LongStream filter() in Java with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report LongStream filter(LongPredicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Syntax : LongStream filter(LongPredicate predicate) Where, LongStream is a sequence of primitive long-valued elements. LongPredicate represents a predicate (boolean-valued function) of one long-valued argument and the function returns the new stream. Example 1 : filter() method on LongStream. Java // Java code for LongStream filter // (LongPredicate predicate) to get a stream // consisting of the elements of this // stream that match the given predicate. import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream = LongStream.of(3L, 5L, 6L, 8L, 9L); // Using LongStream filter(LongPredicate predicate) // to get a stream consisting of the // elements that gives remainder 2 when // divided by 3 stream.filter(num -> num % 3 == 2) .forEach(System.out::println); } } Output : 5 8 Example 2 : filter() method on LongStream. Java // Java code for LongStream filter // (LongPredicate predicate) to get a stream // consisting of the elements of this // stream that match the given predicate. import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream = LongStream.of(-2L, -1L, 0L, 1L, 2L); // Using LongStream filter(LongPredicate predicate) // to get a stream consisting of the // elements that are greater than 0 stream.filter(num -> num > 0) .forEach(System.out::println); } } Output : 1 2 Comment More infoAdvertise with us Next Article Stream filter() in Java with examples S Sahil_Bansall Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Stream filter() in Java with examples Stream filter(Predicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, but ins 3 min read Stream mapToLong() in Java with examples Stream mapToLong(ToLongFunction mapper) returns a LongStream consisting of the results of applying the given function to the elements of this stream. Stream mapToLong(ToLongFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Strea 2 min read IntStream filter() in Java with examples IntStream filter(IntPredicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, b 2 min read Logger getFilter() Method in Java with Examples The getFilter() method of the Logger class is used to get the current filter for this Logger instance. A Filter is useful to filter out log messages. we can say that filter decide the message gets logged or not. Filters are represented by the Java interface java.util.logging.Filter Syntax: public Fi 2 min read Java Stream findAny() with examples Stream findAny() returns an Optional (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty Optional if the stream is empty. Syntax of findAny()Optional<T> findAny() Parameters1. Optional is a container object which may or may not 4 min read Java Servlet Filter with Example A filter is an object that is invoked at the preprocessing and postprocessing of a request on the server, i.e., before and after the execution of a servlet for filtering the request. Filter API (or interface) includes some methods which help us in filtering requests. To understand the concept of Fil 4 min read Like