给定列表[1, 2, 3]和列表[3, 4],返回[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]。
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
List<Integer> numbers2 = Arrays.asList(3, 4);
numbers1.stream().flatMap(
a -> numbers2.stream().map(b -> new int[]{a, b})
).collect(Collectors.toList()).forEach(c -> System.out.println(Arrays.toString(c)));
}
}
输出:
[1, 3]
[1, 4]
[2, 3]
[2, 4]
[3, 3]
[3, 4]