如何使用各种集合通过组合来自这些不同集合的数据来创建对象
how to use various collections to create objects by combining data from these diverse collections
如何使用各种集合通过组合来自这些不同集合的数据来创建对象
我有 8 个不同的集合,需要 ID 来遍历所有集合以生成组合这些 ID 的对象。
假设我有以下集合 ID 作为输入:
颜色、高度、长度、宽度、材料、厚度、纹理和效果。
而且我想创建一个新的集合所有这些功能的对象。
因此,如果这些输入集合中的每一个都有 2 个项目,那么最后我将得到一个包含 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 256 个对象的新集合,其中包含这些对象的所有可能组合特征.
如何使用 Java 有效地做到这一点?
下面的代码使用了 Streamplify library and assumes that you have a class called Characteristics with accessor methods for your features. See full implementation here.
private static class Feature<T> {
final BiConsumer<Characteristics, T> setter;
final List<T> choices;
Feature(BiConsumer<Characteristics, T> setter, T... choices) {
this.setter = setter;
this.choices = Arrays.asList(choices);
}
}
public static void main(String[] args) {
final List<Feature<?>> features = Arrays.asList(
new Feature<Color>(Characteristics::setColor, Color.BLUE, Color.YELLOW, Color.RED),
new Feature<Integer>(Characteristics::setHeight, 20, 30),
new Feature<Integer>(Characteristics::setLength, 100, 120),
new Feature<Integer>(Characteristics::setWidth, 60, 80),
new Feature<String>(Characteristics::setMaterial, "wood", "metal", "glass"),
new Feature<Integer>(Characteristics::setThickness, 4, 6),
new Feature<String>(Characteristics::setTexture, "smooth", "grainy", "velvety"),
new Feature<Effect>(Characteristics::setEffect, new Shadow(), new BoxBlur())
);
int[] dimensions = features.stream().mapToInt(f -> f.choices.size()).toArray();
List<Characteristics> chrVariants = new CartesianProduct(dimensions)
.stream()
.map(prod -> {
Characteristics chr = new Characteristics();
IntStream.range(0, prod.length)
.forEach(i -> {
Feature f = features.get(i);
f.setter.accept(chr, f.choices.get(prod[i]));
});
return chr;
})
.collect(Collectors.toList());
chrVariants.forEach(chr -> System.out.println(chr));
}
输出:
0x0000ffff, 20, 100, 60, wood, 4, smooth, Shadow
0x0000ffff, 20, 100, 60, wood, 4, smooth, BoxBlur
0x0000ffff, 20, 100, 60, wood, 4, grainy, Shadow
0x0000ffff, 20, 100, 60, wood, 4, grainy, BoxBlur
0x0000ffff, 20, 100, 60, wood, 4, velvety, Shadow
0x0000ffff, 20, 100, 60, wood, 4, velvety, BoxBlur
0x0000ffff, 20, 100, 60, wood, 6, smooth, Shadow
0x0000ffff, 20, 100, 60, wood, 6, smooth, BoxBlur
0x0000ffff, 20, 100, 60, wood, 6, grainy, Shadow
0x0000ffff, 20, 100, 60, wood, 6, grainy, BoxBlur
0x0000ffff, 20, 100, 60, wood, 6, velvety, Shadow
0x0000ffff, 20, 100, 60, wood, 6, velvety, BoxBlur
0x0000ffff, 20, 100, 60, metal, 4, smooth, Shadow
0x0000ffff, 20, 100, 60, metal, 4, smooth, BoxBlur
...
如何使用各种集合通过组合来自这些不同集合的数据来创建对象
我有 8 个不同的集合,需要 ID 来遍历所有集合以生成组合这些 ID 的对象。
假设我有以下集合 ID 作为输入: 颜色、高度、长度、宽度、材料、厚度、纹理和效果。
而且我想创建一个新的集合所有这些功能的对象。
因此,如果这些输入集合中的每一个都有 2 个项目,那么最后我将得到一个包含 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 256 个对象的新集合,其中包含这些对象的所有可能组合特征.
如何使用 Java 有效地做到这一点?
下面的代码使用了 Streamplify library and assumes that you have a class called Characteristics with accessor methods for your features. See full implementation here.
private static class Feature<T> {
final BiConsumer<Characteristics, T> setter;
final List<T> choices;
Feature(BiConsumer<Characteristics, T> setter, T... choices) {
this.setter = setter;
this.choices = Arrays.asList(choices);
}
}
public static void main(String[] args) {
final List<Feature<?>> features = Arrays.asList(
new Feature<Color>(Characteristics::setColor, Color.BLUE, Color.YELLOW, Color.RED),
new Feature<Integer>(Characteristics::setHeight, 20, 30),
new Feature<Integer>(Characteristics::setLength, 100, 120),
new Feature<Integer>(Characteristics::setWidth, 60, 80),
new Feature<String>(Characteristics::setMaterial, "wood", "metal", "glass"),
new Feature<Integer>(Characteristics::setThickness, 4, 6),
new Feature<String>(Characteristics::setTexture, "smooth", "grainy", "velvety"),
new Feature<Effect>(Characteristics::setEffect, new Shadow(), new BoxBlur())
);
int[] dimensions = features.stream().mapToInt(f -> f.choices.size()).toArray();
List<Characteristics> chrVariants = new CartesianProduct(dimensions)
.stream()
.map(prod -> {
Characteristics chr = new Characteristics();
IntStream.range(0, prod.length)
.forEach(i -> {
Feature f = features.get(i);
f.setter.accept(chr, f.choices.get(prod[i]));
});
return chr;
})
.collect(Collectors.toList());
chrVariants.forEach(chr -> System.out.println(chr));
}
输出:
0x0000ffff, 20, 100, 60, wood, 4, smooth, Shadow
0x0000ffff, 20, 100, 60, wood, 4, smooth, BoxBlur
0x0000ffff, 20, 100, 60, wood, 4, grainy, Shadow
0x0000ffff, 20, 100, 60, wood, 4, grainy, BoxBlur
0x0000ffff, 20, 100, 60, wood, 4, velvety, Shadow
0x0000ffff, 20, 100, 60, wood, 4, velvety, BoxBlur
0x0000ffff, 20, 100, 60, wood, 6, smooth, Shadow
0x0000ffff, 20, 100, 60, wood, 6, smooth, BoxBlur
0x0000ffff, 20, 100, 60, wood, 6, grainy, Shadow
0x0000ffff, 20, 100, 60, wood, 6, grainy, BoxBlur
0x0000ffff, 20, 100, 60, wood, 6, velvety, Shadow
0x0000ffff, 20, 100, 60, wood, 6, velvety, BoxBlur
0x0000ffff, 20, 100, 60, metal, 4, smooth, Shadow
0x0000ffff, 20, 100, 60, metal, 4, smooth, BoxBlur
...