生成笛卡尔产品Java



我正在尝试加载测试后端服务器,并且正在将一些配置解析为Label对象。我正在尝试生成Label对象的笛卡尔乘积,这将是一个Metric对象。

Label {
String name;
String labe;
}

我有一个从 yaml 配置转换List<List<Label>> labelList,需要通过笛卡尔积转换为List<Metric>。下面是一个示例。

[[name: "office_instance"value: "office_instance_0", name: "office_instance"value: "office_instance_1", name: "office_instance"value: "office_instance_2", name: "office_instance"value: "office_instance_3"], [name: "office_type"value: "office_type_0", name: "office_type"value: "office_type_1", name: "office_type"value: "office_type_2", name: "office_type"value: "office_type_3"], [name: "resource_type"value: "resource_type_0", name: "resource_type"value: "resource_type_1", name: "resource_type"value: "resource_type_2", name: "resource_type"value: "resource_type_3"], [name: "resource"value: "resource_0", name: "resource"value: "resource_1", name: "resource"value: "resource_2", name: "resource"value: "resource_3"], [name: "resource_group_name"value: "resource_group_name_0", name: "resource_group_name"value: "resource_group_name_1", name: "resource_group_name"value: "resource_group_name_2", name: "resource_group_name"value: "resource_group_name_3"], [name: "scope"value: "scope_0", name: "scope"value: "scope_1", name: "scope"value: "scope_2", name: "scope"value: "scope_3"]]

由于这是从配置生成的,因此生成时间序列的代码必须是动态的。在此示例中,labelList的大小为 5,每个单独的列表 4 个标签。因此,指标对象的总数将为 4 * 4 * 4 * 4 * 4。

度量对象的示例将是

[[ name: "office_instance"value: "office_instance_0", name: "office_type"value: "office_type_0", name: "resource_type"value: "resource_type_0", name: "resource"value: "resource_0", name: "resource_group_name"value: "resource_group_name_0", name: "scope"value: "scope_0"][name: "office_instance"value: "office_instance_0", name: "office_type"value: "office_type_0", name: "resource_type"value: "resource_type_0", name: "resource"value: "resource_0", name: "resource_group_name"value: "resource_group_name_0", name: "scope"value: "scope_1"]......]

生成指标有两种方法,一种是通过addLabels方法一次添加一个标签

Metric.newBuilder().addLabels(Label obj).build()

或通过addAllLabels方法添加

Metric.newBuilder().addAllLabels(List<Label> obj).build()

我试图以迭代/递归的方式做到这一点,但它变得非常复杂和混乱。我检查了是否有java8替代方案。如果我能够在第一次传递后获得List<Label>Collection<Label>,我可以使用the addAlLabels方法创建Metric对象并将其添加到最终列表中,而不是创建Metric对象并添加每个label

我在这里引用了Java 8中流的笛卡尔乘积作为流(仅使用流)。 但仍然想不通。任何帮助将不胜感激。

编辑:发现番石榴有cartesianProductAPI,这可能会有所帮助,不确定它是否是最优化的。

在我看来,你需要一个递归函数。 这就是我想出的:

public class Cartesian {
public static void main(String[] args) {
List<List<Label>> labelList = Arrays.asList(//
Arrays.asList( //
new Label("office_instance", "office_instance_0"), //
new Label("office_instance", "office_instance_1") //
), //
Arrays.asList( //
new Label("office_type", "office_type_0"), //
new Label("office_type", "office_type_1") //
), //
Arrays.asList( //
new Label("resource_type", "resource_type_0"), //
new Label("resource_type", "resource_type_1") //
)//
);
List<List<Label>> result = generateCombinations(labelList);
System.out.println(result);
}
private static List<List<Label>> generateCombinations(List<List<Label>> labels) {
List<List<Label>> result = new ArrayList<>();
// for each label in first list
// generate combinations of labels from the rest of the list
for (Label label : labels.get(0)) {
if (labels.size() > 1) {
for (List<Label> entry : generateCombinations(labels.subList(1, labels.size()))) {
entry.add(label);
result.add(entry);
}
} else {
// base case
result.add(new ArrayList<>(Arrays.asList(label)));
}
}
return result;
}
private static class Label {
String name;
String label;
Label(String name, String label) {
this.name = name;
this.label = label;
}
@Override
public String toString() {
return "(name: " + name + ", label: " + label + ")";
}
}
}

为了简洁起见,我没有使用完整列表。

输出(手动格式化):

[
[
(name: resource_type, label: resource_type_0),
(name: office_type, label: office_type_0),
(name: office_instance, label: office_instance_0)
],
[
(name: resource_type, label: resource_type_1),
(name: office_type, label: office_type_0),
(name: office_instance, label: office_instance_0)
],
[
(name: resource_type, label: resource_type_0),
(name: office_type, label: office_type_1),
(name: office_instance, label: office_instance_0)
],
[
(name: resource_type, label: resource_type_1),
(name: office_type, label: office_type_1),
(name: office_instance, label: office_instance_0)
],
[
(name: resource_type, label: resource_type_0),
(name: office_type, label: office_type_0),
(name: office_instance, label: office_instance_1)
],
[
(name: resource_type, label: resource_type_1),
(name: office_type, label: office_type_0),
(name: office_instance, label: office_instance_1)
],
[
(name: resource_type, label: resource_type_0),
(name: office_type, label: office_type_1),
(name: office_instance, label: office_instance_1)
],
[
(name: resource_type, label: resource_type_1),
(name: office_type, label: office_type_1),
(name: office_instance, label: office_instance_1)
]
]

相关内容

  • 没有找到相关文章

最新更新