我有以下两个列表
List<String> color = new ArrayList<>();
color.add("red");
color.add("white");
color.add("pink");
List<Specification> specList = new ArrayList<>();
Specification soundSystem = new Specification("MusicSysten", "harman Kardon");
Specification price = new Specification("Price", "50000");
specList.add(soundSystem);
specList.add(price);
我想为每个颜色项创建一个规范对象的新实例,并将其添加到specList中。我尝试过类似的方法,但是它不起作用。
List<Specification> output = color.stream().map(c -> specList.add(new Specification("color", c))).collect(Collectors.toList());
我想要的输出类似
List<List<Specification>> output = [[(new Specification("MusicSysten", "harman Kardon"), new Specification("Price", "50000"), new Specification("Color", "red"))], [(new Specification("MusicSysten", "harman Kardon"), new Specification("Price", "50000"), new Specification("Color", "white"))], [(new Specification("MusicSysten", "harman Kardon"), new Specification("Price", "50000"), new Specification("Color", "pink"))] ]
有什么建议吗?
答案 0 :(得分:2)
您正在映射到新的规范,因此列表应为Specification
类型。您只需映射到一个对象,然后让收集器构建列表即可。然后,将该列表添加到specList。
请注意,如果仅使用soundSystem
和price
的相同现有对象,除非假定它们是不可变的,否则可能会引起问题。因此,我向规范类添加了一个复制方法,以创建每个规范的新对象。而且由于List.of
产生了不可变的列表,所以我将其传递给ArrayList
来创建常规列表。
List<List<Specification>> output = color.stream()
.map(c -> new ArrayList<>(List.of(soundSystem,copy(), price.copy(),
new Specification("color", c))))
.collect(Collectors.toList());
output.forEach(System.out::println);
打印
[{MusicSysten, harman Kardon}, {Price, 50000}, {color, red}]
[{MusicSysten, harman Kardon}, {Price, 50000}, {color, white}]
[{MusicSysten, harman Kardon}, {Price, 50000}, {color, pink}]
规格等级
class Specification {
String attribute1;
String attribute2;
public Specification(String attribute1, String attribute2) {
this.attribute1 = attribute1;
this.attribute2 = attribute2;
}
public String toString() {
return "{" + attribute1 + ", " + attribute2 + "}";
}
public Specification copy() {
return new Specification(this.attribute1, this.attribute2);
}
}
答案 1 :(得分:2)
根据您的说明,您似乎需要以下内容(假设您使用的是Java 9或更高版本,并且可以使用方便的List.of
工厂方法):
Stream.of("red", "white", "pink")
.map(c -> List.of(
new Specification("MusicSysten", "harman Kardon"),
new Specification("Price", "50000"),
new Specification("Color", c)
)).collect(toList());
答案 2 :(得分:2)
尝试一下。
List<List<Specification>> output = color.stream()
.map(c -> Stream.concat(specList.stream(), Stream.of(new Specification("color", c)))
.collect(Collectors.toList()))
.collect(Collectors.toList());
System.out.println(output);
结果:
[[Specification(MusicSysten, harman Kardon), Specification(Price, 50000), Specification(color, red)], [Specification(MusicSysten, harman Kardon), Specification(Price, 50000), Specification(color, white)], [Specification(MusicSysten, harman Kardon), Specification(Price, 50000), Specification(color, pink)]]
答案 3 :(得分:1)
@WJS解决方案将起作用。如果您需要先添加soundSystem和price,则可以使用foreach流方法。如果已知,您还可以使用总大小启动ArrayList。
color.stream()
.map(c -> new Specification("color", c))
.foreach(specList::add);
答案 4 :(得分:0)
如果您使用的是Java 9或更高版本,则可以尝试
List<List<Specification>> output = color.stream()
.map(c -> List.of(soundSystem.copy(), price.copy(), new Specification("color", c)))
.collect(Collectors.toList());
copy()
类上的Specification
方法可能类似于@WJS在其答案中提到的