Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Stream Practice | enum & Stream API
course content

Contenido del Curso

Java Data Structures

Stream PracticeStream Practice

It's time for you to practice with Stream API. You have a few straightforward tasks involving the use of various Stream API methods. This time, I won't provide you with specific tasks. Instead, I'll give you a list to work with and what the expected result should be. In other words, your goal is to perform operations on the list to achieve the expected outcome.

This type of task can help you develop the skill of achieving results without a clear specification. You'll only see the input data and the expected result.

You need to use Stream API to accomplish these tasks!

Note

I strongly recommend working with this code in an IDE, as there you'll be able to see various methods, and IntelliJ will assist you with code generation or method references. However, no one prohibits you from completing this task here on the platform—it's entirely up to you!

Task 1:

Input:

java

main.java

Use Stream API to square each number in the list and collect the result into a new list. Expected Output:

Use `map()` and `toList()` methods here.

Task 2:

Input:

java

main.java

Use Stream API to find the length of the longest name in the list. Expected Output:

Use `map()` and `max()` intermediate methods, and the `get()` terminal method.

Task 3

Input:

java

main.java

Use Stream API to count the total number of distinct words (case-insensitive) in all the sentences. Expected Output:

Use `flatMap()` and `distinct()` intermediate methods, and the `count()` terminal method.

Task 4:

Input:

java

main.java

Use Stream API to find the concatenation of the first two words that have even lengths.

Note

Use collect(Collectors.joining()) to concatenate the words.

Expected Output:

Use `filter()` and `limit()` intermediate methods, and the `collect()` terminal method.

Task 5:

Input:

java

main.java

Use Stream API to find the sum of the squares of even numbers in the list.

Note

Use mapToInt instead of map to get access to the sum terminal method.

Expected Output:

Use `mapToInt()` and `filter()` intermediate methods, and the `sum()` terminal method.

Task 6:

Input:

java

main.java

Use Stream API to group words by their lengths and collect the result into a Map.

Note

You can use collect(Collectors.groupingBy(e -> e.length())) to collect stream into a Map.

Expected Output:

Use the `collect()` terminal method.

Good job!

If you've completed all the tasks, well done!

These problems can be solved not only by the methods I provided in the solution block. You can solve them in the way that suits you best. You may write separate methods and use them in the map() method, or you may not use the Stream API at all.

You may have also noticed that some methods from the tasks were not covered earlier. In such cases, it's worth opening IntelliJ IDEA and exploring how these tasks can be accomplished using different methods. If you have questions about the methods or need additional information, feel free to ask in the Community Chat, where other users or course authors can help you.

In programming, learning never stops, and discovering new ways to solve various problems is part of the journey. Don't hesitate to explore the internet to find new information or read the documentation where all the methods are described. Good luck!

¿Todo estuvo claro?

Sección 4. Capítulo 5
course content

Contenido del Curso

Java Data Structures

Stream PracticeStream Practice

It's time for you to practice with Stream API. You have a few straightforward tasks involving the use of various Stream API methods. This time, I won't provide you with specific tasks. Instead, I'll give you a list to work with and what the expected result should be. In other words, your goal is to perform operations on the list to achieve the expected outcome.

This type of task can help you develop the skill of achieving results without a clear specification. You'll only see the input data and the expected result.

You need to use Stream API to accomplish these tasks!

Note

I strongly recommend working with this code in an IDE, as there you'll be able to see various methods, and IntelliJ will assist you with code generation or method references. However, no one prohibits you from completing this task here on the platform—it's entirely up to you!

Task 1:

Input:

java

main.java

Use Stream API to square each number in the list and collect the result into a new list. Expected Output:

Use `map()` and `toList()` methods here.

Task 2:

Input:

java

main.java

Use Stream API to find the length of the longest name in the list. Expected Output:

Use `map()` and `max()` intermediate methods, and the `get()` terminal method.

Task 3

Input:

java

main.java

Use Stream API to count the total number of distinct words (case-insensitive) in all the sentences. Expected Output:

Use `flatMap()` and `distinct()` intermediate methods, and the `count()` terminal method.

Task 4:

Input:

java

main.java

Use Stream API to find the concatenation of the first two words that have even lengths.

Note

Use collect(Collectors.joining()) to concatenate the words.

Expected Output:

Use `filter()` and `limit()` intermediate methods, and the `collect()` terminal method.

Task 5:

Input:

java

main.java

Use Stream API to find the sum of the squares of even numbers in the list.

Note

Use mapToInt instead of map to get access to the sum terminal method.

Expected Output:

Use `mapToInt()` and `filter()` intermediate methods, and the `sum()` terminal method.

Task 6:

Input:

java

main.java

Use Stream API to group words by their lengths and collect the result into a Map.

Note

You can use collect(Collectors.groupingBy(e -> e.length())) to collect stream into a Map.

Expected Output:

Use the `collect()` terminal method.

Good job!

If you've completed all the tasks, well done!

These problems can be solved not only by the methods I provided in the solution block. You can solve them in the way that suits you best. You may write separate methods and use them in the map() method, or you may not use the Stream API at all.

You may have also noticed that some methods from the tasks were not covered earlier. In such cases, it's worth opening IntelliJ IDEA and exploring how these tasks can be accomplished using different methods. If you have questions about the methods or need additional information, feel free to ask in the Community Chat, where other users or course authors can help you.

In programming, learning never stops, and discovering new ways to solve various problems is part of the journey. Don't hesitate to explore the internet to find new information or read the documentation where all the methods are described. Good luck!

¿Todo estuvo claro?

Sección 4. Capítulo 5
some-alt