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
1234567891011package 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[1, 4, 9, 16, 25]
Use map()
and toList()
methods here.
java9912345678910111213package 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);List<Integer> result = numbers.stream().map(e -> e * e).toList();System.out.println(result);// Write your code here}}
Task 2:
Input:
Main
1234567891011package 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:
java7
Use map()
and max()
intermediate methods, and the get()
terminal method.
java991234567891011121314151617package 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");Integer result = names.stream().map(e -> e.length()).max((integer, newInteger) -> Integer.compare(integer, newInteger)).get();System.out.println(result);// Write your code here}}
Task 3
Input:
Main
123456789101112131415package 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:
java37
Use flatMap()
and distinct()
intermediate methods, and the count()
terminal method.
java991234567891011121314151617181920package 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.");long count = sentences.stream().flatMap(e -> Arrays.stream(e.split(" "))).distinct().count();System.out.println(count);// Write your code here}}
Task 4:
Input:
Main
1234567891011package 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:
javabananacherry
Use filter()
and limit()
intermediate methods, and the collect()
terminal method.
java991234567891011121314151617package com.example;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class Main {public static void main(String[] args) {List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");String result = words.stream().filter(e -> e.length() % 2 == 0).limit(2).collect(Collectors.joining());System.out.println(result);// Write your code here}}
Task 5:
Input:
Main
1234567891011package 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:
java220
Use mapToInt()
and filter()
intermediate methods, and the sum()
terminal method.
java9912345678910111213141516package 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);int sum = numbers.stream().mapToInt(e -> e * e).filter(e -> e % 2 == 0).sum();System.out.println(sum);// Write your code here}}
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.
Thanks for your feedback!
Ask AI
Ask anything or try one of the suggested questions to begin our chat