交叉连接两个列表java

Aka*_*ran 2 list nested-loops java-8

我有一个ABC类,它包含两个整数字段

public class ABC{
  private Integer x;
  private Integer y;

   // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

我有两个列表:xValues和yValues,它们分别包含x和y值的整数列表.

List<Integer> xValues = fetchAllXValues();  //say list xValues contains {1,2,3}
List<Integer> yValues = fetchAllYValues();  //say list yValues contains {7,8,9}
Run Code Online (Sandbox Code Playgroud)

现在我想要的是使用xValues列表的每个值与yValues列表的每个值创建一个ABC对象.我不想使用嵌套for循环.什么是更有效的解决方法?

ABC的示例输出对象是:

     ABC(1,7);
     ABC(1,8);
     ABC(1,9);
     ABC(2,7);
     ABC(2,8);
     ABC(2,9);
     ABC(3,7);
     ABC(3,8);
     ABC(3,9);
Run Code Online (Sandbox Code Playgroud)

Ser*_*tin 5

迭代第一个列表,并在每次迭代迭代第二个列表:

xValues.stream()
    .flatMap(x -> yValues.stream().map(y -> new ABC(x, y)))
    .collect(toList());
Run Code Online (Sandbox Code Playgroud)