繁体   English   中英

Java8 流:创建一个新的 object 并将其添加到列表中,同时迭代另一个列表

[英]Java8 streams : Creating a new object and adding it to list while iterating over another list

我有两个列表如下

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);

我想为每个颜色项创建一个规范 object 的新实例并将其添加到规范列表中。 我试过这样的东西,但它不工作。

List<Specification> output = color.stream().map(c -> specList.add(new Specification("color", c))).collect(Collectors.toList());

我想要的 output 是这样的

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"))] ]

有什么建议吗?

您正在映射到一个新的规范,因此 List 的类型应该是Specification 您只需将 map 转换为 object 并让收集器构建列表。 然后将该列表添加到 specList。

请注意,如果您只是继续使用soundSystemprice的相同现有对象,这可能会导致问题,除非它们应该是不可变的。 所以我在规范 class 中添加了一个复制方法来创建每个规范的新对象。 由于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

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);
    }
}

根据您的说明,您似乎需要以下内容(假设您使用的是 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());

@WJS 解决方案将起作用。 如果您需要先添加声音系统和价格,您可以使用 foreach stream 方法。 如果已知,您还可以使用总大小启动 ArrayList。

color.stream()
     .map(c -> new Specification("color", c))
     .foreach(specList::add);

尝试这个。

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)]]

如果您使用的是 Java 9 或更高版本,您可以尝试

List<List<Specification>> output = color.stream()
     .map(c -> List.of(soundSystem.copy(), price.copy(), new Specification("color", c)))
     .collect(Collectors.toList());

Specification class 上的copy()方法可能与@WJS 在他的回答中提到的相似

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM