Use map() to transform each element into its cube, then filter() to keep only the values greater than 50. The order matters: the filter checks the cubed value, not the original number.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CubeAndFilter {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(4, 5, 6, 7, 1, 2, 3);
List<Integer> result = list.stream()
.map(i -> i * i * i)
.filter(i -> i > 50)
.collect(Collectors.toList());
System.out.println(result);
}
}[64, 125, 216, 343]
Interviewers may ask you to swap the steps: "Filter numbers greater than 3, then cube them". Show that filter() before map() changes the result, and that filtering first is cheaper when it removes many elements.
A concise interview answer is
I map each element to its cube with map(i -> i * i * i) and then filter(i -> i > 50), so the filter checks the cubed value. The order of map and filter matters, and for large numbers I'd switch to long to avoid overflow.
Last updated: 11 November 2026
Previous question: Write a Java 8 program to concatenate two streams.
Next question: Write a Java 8 program to sort an array and convert it into a stream.