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

Conteúdo do Curso

Java Data Structures

Java Data Structures

1. Basic Data Structures
2. Additional Data Structures
3. Map
4. enum & Stream API

bookStream 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

copy
1234567891011
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); //write your code here } }

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

Task 2:

Input:

java

main

copy
1234567891011
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eva"); //write your code here } }

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

Task 3

Input:

java

main

copy
123456789101112131415
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> sentences = Arrays.asList( "Java Stream API provides a fluent interface for processing sequences of elements.", "It supports functional-style operations on streams of elements, such as map-reduce transformations.", "In this exercise, you need to count the total number of words in all sentences." ); //write your code here } }

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

Task 4:

Input:

java

main

copy
1234567891011
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry"); //write your code here } }

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:

Task 5:

Input:

java

main

copy
1234567891011
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); //write your code here } }

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:

Task 6:

Input:

java

main

copy
1234567891011
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry"); //write your code here } }

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:

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!

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 5
some-alt