Contenu du cours
Java Data Structures
Java Data Structures
Practicing with Stream API
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, no specific tasks will be provided. 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!
Task 1:
Input:
Main
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:
java
Use map()
and toList()
methods here.
java
Task 2:
Input:
Main
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:
java
Use map()
and max()
intermediate methods, and the get()
terminal method.
java
Task 3
Input:
Main
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:
java
Use flatMap()
and distinct()
intermediate methods, and the count()
terminal method.
java
Task 4:
Input:
Main
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.
Expected Output:
java
Use filter()
and limit()
intermediate methods, and the collect()
terminal method.
java
Task 5:
Input:
Main
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.
Expected Output:
java
Use mapToInt()
and filter()
intermediate methods, and the sum()
terminal method.
java
Good job!
If you've completed all the tasks, well done!
Some methods in the tasks may not have been covered earlier. To explore them, open IntelliJ IDEA and check their functionality.
Programming is about continuous learning, so explore new solutions, search online, and refer to documentation when needed.
Merci pour vos commentaires !