Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Selecting Random Products Within a Category | Terminal Operations in the Stream API
Introduction to Stream API

bookChallenge: Selecting Random Products Within a Category

Task

Swipe to start coding

Imagine you have a list of products from an online store. Your goal is to group them by category, pick a random product from each group, and display the result.

  1. Group the list of products by category using groupingBy().
  2. For each category (productList), get the list of products using the stream() method and select a random product with findAny().
  3. Check if the Optional contains a value using ifPresent().
  4. If Optional has a value, print the information in the following format: "Category: {category} => {product}".

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 11
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookChallenge: Selecting Random Products Within a Category

Swipe to show menu

Task

Swipe to start coding

Imagine you have a list of products from an online store. Your goal is to group them by category, pick a random product from each group, and display the result.

  1. Group the list of products by category using groupingBy().
  2. For each category (productList), get the list of products using the stream() method and select a random product with findAny().
  3. Check if the Optional contains a value using ifPresent().
  4. If Optional has a value, print the information in the following format: "Category: {category} => {product}".

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Β 3. ChapterΒ 11
single

single

some-alt