Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Selecting the Best Cars on the Production Line | Intermediate Operations in Stream API
Stream API
course content

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

Task

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().

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 6
toggle bottom row

book
Challenge: Selecting the Best Cars on the Production Line

Task

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().

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt