Java Reference
In-Depth Information
You can solve this problem by using map with a lambda that takes a number and returns the
square of the number:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squares =
numbers.stream()
.map(n -> n * n)
.collect(toList());
2. Given two lists of numbers, how would you return all pairs of numbers? For example, given a
list [1, 2, 3] and a list [3, 4] you should return [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]. For
simplicity, you can represent a pair as an array with two elements.
Answer:
You could use two maps to iterate on the two lists and generate the pairs. But this would return
a Stream<Stream<Integer[]>>. What you need to do is flatten the generated streams to result in
a Stream<Integer[]>. This is what flatMap is for:
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
List<Integer> numbers2 = Arrays.asList(3, 4);
List<int[]> pairs =
numbers1.stream()
.flatMap(i -> numbers2.stream()
.map(j -> new int[]{i, j})
)
.collect(toList());
3. How would you extend the previous example to return only pairs whose sum is divisible by 3?
For example, (2, 4) and (3, 3) are valid.
Answer:
You saw earlier that filter can be used with a predicate to filter elements from a stream. Because
after the flatMap operation you have a stream of int[] that represent a pair, you just need a
predicate to check to see if the sum is divisible by 3:
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
List<Integer> numbers2 = Arrays.asList(3, 4);
List<int[]> pairs =
numbers1.stream()
 
Search WWH ::




Custom Search