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

Kursinnehåll

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

Uppgift

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

Lösning

java

solution

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6
toggle bottom row

book
Challenge: Selecting the Best Cars on the Production Line

Uppgift

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

Lösning

java

solution

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt