如何使用Java streams将多个列表转换为单个列表?

如何使用Java streams将多个列表转换为单个列表?,java,java-stream,Java,Java Stream,我有一个类A,有多个列表成员 class A { List<X> xList; List<Y> yList; List<Z> zList; // getters and setters } class X { String desc; String xtype; // getters and setters } class Y { String name

我有一个类
A
,有多个
列表
成员

class A {
    List<X> xList;
    List<Y> yList;
    List<Z> zList;
    
    // getters and setters
}

class X {
     String desc;
     String xtype;
    
     // getters and setters    
}

class Y {   
    String name;
    String ytype;
    
    //getters and setters
}

class Z {
    String description;
    String ztype;
    
    // getters and setters    
}
我需要遍历class
A
中的各种列表,创建class
B
对象并添加到如下列表中:

public void convertList(A a) {  
   List<B> b = new ArrayList<>();
    
    if (!a.getXList().isEmpty()) {
      for (final X x : a.getXList()) {
           b.add(new B(x.getDesc(), x.getXType()));
      }
    }
    
    if (!a.getYList().isEmpty()) {
      for (final Y y : a.getYList()) {
           b.add(new B(y.getName(), y.getYType()));
      }
    }
    
    if (!a.getZList().isEmpty()) {
      for (final Z z : a.getZList()) {
           b.add(new B(z.getDescription(), z.getZType()));
      }
    }    
}
publicsvoid转换列表(A){
列表b=新的ArrayList();
如果(!a.getXList().isEmpty()){
对于(最终X:a.getXList()){
b、 添加(新的b(x.getDesc(),x.getXType());
}
}
如果(!a.getYList().isEmpty()){
for(最终Y:a.getYList()){
b、 添加(新的b(y.getName(),y.getYType());
}
}
如果(!a.getZList().isEmpty()){
for(最终Z:a.getZList()){
b、 添加(新的b(z.getDescription(),z.getZType());
}
}    
}
这里重复了if和for循环

如何使用Java流实现这一点


注意:类
X
Y
Z
之间没有关系,也没有公共接口。

由于您的
X
Y
Z
类型没有公共的超类型,因此必须将它们转换为一些公共类型,例如
Map.Entry

您可以创建所有名称和类型对的
,然后将其映射到
B
的实例:

List<B> b =
    Stream.of(
        a.getXList().stream().map(x -> new SimpleEntry<>(x.getDesc(),x.getXType())),
        a.getYList().stream().map(y -> new SimpleEntry<>(y.getName(),y.getYType())),
        a.getZList().stream().map(z -> new SimpleEntry<>(z.getDescription(),z.getZType())))
          .flatMap(Function.identity())
          .map(e -> new B(e.getKey(), e.getValue()))
          .collect(Collectors.toList());

您正在为X、Y、Z类使用不同的属性,并且没有公共接口,您可以在列表中逐个添加

b.addAll(a.getXList().stream().map(x ->new B(x.getDesc(), x.getXType())).collect(Collectors.toList()));
b.addAll(a.getYList().stream().map(y ->new B(y.getName(), y.getYType())).collect(Collectors.toList()));
b.addAll(a.getZList().stream().map(z ->new B(z.getDescription(), z.getZType())).collect(Collectors.toList()));

由于没有公共接口,因此必须使用
forEach
方法迭代每个列表

a.getXList().forEach(i -> b.add(new B(i.getDesc(), i.getXType())));
a.getYList().forEach(i -> b.add(new B(i.getName(), i.getYType())));
a.getZList().forEach(i -> b.add(new B(i.getDescription(), i.getZType())));

您可以像下面这样使用
Stream.concat()

public List<B> convertList (A a) {
    return Stream.concat(Stream.concat(a.getXList().stream().map(x -> new B(x.getDesc(), x.getXType()))
            , a.getYList().stream().map(y -> new B(y.getName(), y.getYType())))
            , a.getZList().stream().map(z -> new B(z.getDescription(), z.getZType()))).collect(Collectors.toList());
}
公共列表转换列表(A){
返回Stream.concat(Stream.concat(a.getXList().Stream().map(x->newb(x.getDesc(),x.getXType()))
,a.getYList().stream().map(y->new B(y.getName(),y.getYType()))
,a.getZList().stream().map(z->new B(z.getDescription(),z.getZType()).collect(Collectors.toList());
}

您是否尝试过使用FlatMap,如果没有,请查看此处。X、Y和Z是否扩展了相同的基类或实现了同时包含getName和getType的相同接口?请正确回答这个问题,因为您有多个列表,但将
X、Z、Y
作为一个类型传递,但需要一个
B
列表@JoakimDanielson更新了这个问题。没有基类或接口。这些类中的属性都不同。我需要根据每个类的getter转换为类B。@Hadi J我在lambda表达式中得到了错误的返回类型。B无法转换为RI。我在本地机器上运行了这个版本。它对meMy有效。很糟糕,我的大括号放错地方了。这看起来不错。谢谢OP需要
JavaStreams
您可以在非流答案中提到的答案。生成SimpleEntry并将其映射到B有什么意义?
a.getXList().forEach(i -> b.add(new B(i.getDesc(), i.getXType())));
a.getYList().forEach(i -> b.add(new B(i.getName(), i.getYType())));
a.getZList().forEach(i -> b.add(new B(i.getDescription(), i.getZType())));
public List<B> convertList (A a) {
    return Stream.concat(Stream.concat(a.getXList().stream().map(x -> new B(x.getDesc(), x.getXType()))
            , a.getYList().stream().map(y -> new B(y.getName(), y.getYType())))
            , a.getZList().stream().map(z -> new B(z.getDescription(), z.getZType()))).collect(Collectors.toList());
}