DoubleStream filter() method in Java


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

Updated on: 30-Jul-2019

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements