Return to site
JAVA CERTIFICATION QUESTION: Stream data filter
JAVA CERTIFICATION QUESTION: Stream data filter
=>Filtering a stream returns a stream consisting of the elements of this stream that match the given predicate.
·
Given
1. import java.util.stream.DoubleStream; 2. 3. public class TestDoubleSTream{ 4. 5. public static void main(String[] args){ 6. 7. DoubleStream doubles = DoubleStream.of(0.45,0.42,0.49); 8. System.out.print(doubles.filter(e->e>0.45).count()); 9. } 10.}
What is the output?
- A) 1
- B) 2
- C) Compilation fails since we haven't imported predicate.
- D) Compilation fails due to error at line 7.
- E) Compilation fails due to error at line 8.
#java #certificationquestion #ocp
At line 7, we have created a DoubleStream with three doubles.
At line 8 we have used the filter method which lets pass only the values matching values strictly greater than 0.45.
So when count invoked, filter will result a stream with single value.
Hence the number of elements in the stream which count invoked is 1.
·Ɐ ɹǝʍsuɐ sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ :uoᴉsnʅɔuoꓛ