Which line can replace line 18 without changing the output of the program?
1: class Car {// w w w. j a v a2 s . com 2: private int number; 3: public Car(int n) { 4: number = n; 5: } 6: public int getNumber() { 7: return number; 8: } 9: public boolean isNew() { 10: return number < 4*60; 11: } 12: } 13: public class Main { 14: public static void main(String[] args) { 15: Stream<Car> runners = Stream.of(new Car(250), 16: new Car(600), new Car(201)); 17: long count = runners 18: .filter(Car::isNew) 19: .count(); 20: System.out.println(count); 21: } 22: }
A. .map(Car::isNew) B. .mapToBool(Car::isNew) .filter(b -> b == true) C. .mapToBoolean(Car::isNew) .filter(b -> b == true) D. None of the above
D.
There is no built-in method to map a value to a boolean primitive.
Therefore, Options B and C don't even compile, so they are incorrect.
Option A does compile as it maps a Car to a Boolean.
It doesn't actually filter()
the stream to eliminate any values, so the output is not the same.
It prints 3 instead of 1.
None of these are correct, making Option D the answer.