Monday, November 18, 2024
Google search engine
HomeLanguagesJavaDoubleStream filter() in Java with examples

DoubleStream filter() in Java with examples

DoubleStream filter(DoublePredicate 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 :

DoubleStream filter(DoublePredicate predicate)

Parameters :

  1. DoubleStream : A sequence of primitive double-valued elements.
  2. DoublePredicate : A predicate (boolean-valued function) of one double-valued argument.

Return Value : The function returns the new stream.

Example 1 : filter() method on DoubleStream.




// Java code for DoubleStream filter
// (DoublePredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an DoubleStream
        DoubleStream stream = DoubleStream.of(3.6, 5.4, 6.6, 8.1, 9.7);
  
        // Using DoubleStream filter(DoublePredicate predicate)
        // to get a stream consisting of the
        // elements that are greater than 5.7
        stream.filter(num -> num > 5.7)
            .forEach(System.out::println);
    }
}


Output:

6.6
8.1
9.7

Example 2 : filter() method on DoubleStream.




// Java code for DoubleStream filter
// (DoublePredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an DoubleStream
        DoubleStream stream = DoubleStream.of(3.6, 5.4, 6.6, 8.1, 9.7);
  
        // Using DoubleStream filter(DoublePredicate 
        // predicate) to get a stream consisting of 
        // the elements that when divided by 2
        // gives quotient > 2.3
        stream.filter(num -> num / 2.0 > 2.3)
            .forEach(System.out::println);
    }
}


Output:

5.4
6.6
8.1
9.7
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments