Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Selecting the Best Cars on the Production Line | Intermediate Operations in Stream API
Stream API
course content

Зміст курсу

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Challenge: Selecting the Best Cars on the Production Line

Завдання

Swipe to start coding

At the factory, you need to process a list of cars, filter them based on mileage, sort them according to specific criteria, and display the results.

Each car is represented by the Car class, which contains an id as a unique identifier, a model specifying the car’s name, a year indicating when it was manufactured, and a mileage field representing the total distance it has traveled.

  1. Filter the cars, keeping only those with a mileage of 30,000 miles or less using the filter() method.
  2. Sort the cars by year in ascending order using comparingInt() method.
  3. Once the cars are sorted by year, reverse the order to have the newest cars first using reversed() method.
  4. If two cars have the same year, sort them by mileage in ascending order using thenComparing() method.
  5. Convert each car object to a string representation, using the toString() method.
  6. Print the final list to the console, using a method reference to println().

Рішення

java

solution

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
toggle bottom row

book
Challenge: Selecting the Best Cars on the Production Line

Завдання

Swipe to start coding

At the factory, you need to process a list of cars, filter them based on mileage, sort them according to specific criteria, and display the results.

Each car is represented by the Car class, which contains an id as a unique identifier, a model specifying the car’s name, a year indicating when it was manufactured, and a mileage field representing the total distance it has traveled.

  1. Filter the cars, keeping only those with a mileage of 30,000 miles or less using the filter() method.
  2. Sort the cars by year in ascending order using comparingInt() method.
  3. Once the cars are sorted by year, reverse the order to have the newest cars first using reversed() method.
  4. If two cars have the same year, sort them by mileage in ascending order using thenComparing() method.
  5. Convert each car object to a string representation, using the toString() method.
  6. Print the final list to the console, using a method reference to println().

Рішення

java

solution

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt