DoubleStream filter() method in Java

Java 8ProgrammingObject Oriented Programming

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

The syntax is as follows

DoubleStream filter(DoublePredicate predicate)

The parameter predicate is a stateless predicate to apply to each element to determine if it should be included.

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

import java.util.stream.DoubleStream;

Create a DoubleStream and add some elements

DoubleStream doubleStream = DoubleStream.of(20.5, 35.7, 50.8, 67.9, 89.8, 93.1);

Filter and display the element equal to a 50.8, if it’s present

doubleStream.filter(a -> a == 50.8)

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

Example

 Live Demo

import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(20.5, 35.7, 50.8, 67.9, 89.8, 93.1);
      doubleStream.filter(a -> a == 50.8).forEach(System.out::println);
   }
}

Output

50.8
raja
Published on 26-Mar-2019 12:38:13
Advertisements