D
D
devpy142021-06-03 10:01:49
Iron
devpy14, 2021-06-03 10:01:49

How, using a Stream, to add the elements of a List to another sheet, while returning the added elements?

There is a list of cars
List<Car> cars = new ArrayList<>();
as well as the addCars method, which takes a list of cars and whose essence is to add only BMW cars to the collection, after which the method returns all the added cars, and if no cars have been added, then an empty Optional. You need to implement this using Stream.API

Stuck at this point:

public Optional<Car> addCars(List<Car> inputCars) {

        inputCars.stream()
                .filter(car -> car.getMark().equals(Mark.BMW)
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tim3, 2019-02-09
@Marsmaksimus

operatives immediately 16
Prots Risen 7 1700x norms, I have one in boost 3.9 max temperature games + stream up to 65 degrees, cooling is not dropsy.
1070 you can take it, I did it :), and it's still under warranty for a year and a half.

S
Sergey Gornostaev, 2021-06-03
@devpy14

public Optional<List<Car>> addCars(List<Car> inputCars) {
    retrurn Optional.of(inputCars.stream()
            .filter(Objects::nonNull)
            .filter(car -> Mark.BMW.equals(car.getMark()))
            .collect(Collectors.toList()))
        .filter(cars -> !cars.isEmpty());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question