简体   繁体   English

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

[英]How to convert multiple list into single list using Java streams?

I have an class A with multiple List members.我有一个带有多个List成员的 class 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    
}

And a class B with just 2 attributes:还有一个只有 2 个属性的 class B

class B {
    String name;
    String type;     
}

I need to iterate through the various lists in class A and create class B object and add to a list like this:我需要遍历 class A中的各种列表并创建 class B object 并添加到这样的列表中:

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

As the if and for loops are repeated here.因为 if 和 for 循环在这里重复。

How can I achieve this using Java streams?如何使用 Java 流实现此目的?

Note: There is no relation between the classes X , Y and Z and there is no common interface.注意:类XYZ之间没有关系,也没有公共接口。

Since your X , Y and Z types don't have a common super-type, you have to convert them into some common type, such as Map.Entry<String,String> .由于您的XYZ类型没有共同的超类型,因此您必须将它们转换为一些共同的类型,例如Map.Entry<String,String>

You can create a Stream of all pairs of names and types, and then map it to instances of B :您可以创建所有名称和类型对的Stream ,然后将 map 它添加到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());

Or directly generate B instances:或者直接生成B实例:

List<B> b =
    Stream.of(
        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())))
          .flatMap(Function.identity())
          .collect(Collectors.toList());

You can use Stream.concat() like following您可以使用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());
}

Since you don't have a common interface, you would have to use a forEach method to iterate through each list.由于您没有通用接口,因此您必须使用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())));

You are using the different property for X, Y, Z class, and not having common interface, you can add one by one in the list.您使用的是 X、Y、Z class 的不同属性,并且没有通用接口,您可以在列表中一一添加。

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

暂无
暂无

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

相关问题 使用 java 8 个流将列表转换为列表列表 - Convert a list to list of list using java 8 streams 如何使用流将Java bean列表转换为另一个bean列表 - How to convert list of java bean to another list of beans using streams 如何将没有泛型的列表转换为使用java中的流的泛型列表? - How to convert list without generics to list with generics using streams in java? 如何使用Java Streams API进行n次迭代的for循环迭代以实现具有多个动态更改的列表的列表 - How to convert for loop iterating n times to achieve a list using java Streams API with multiple counts changing dynamically 如何将集合<A>转换为列表</a><A1><A>使用java流?</a> - How to convert a Set<A> into a List<A1> using java streams? 如何将 short[] 转换为 List<Short> 在带有流的 Java 中? - How to convert short[] into List<Short> in Java with streams? 如何转换集合<list<map.entry<long, long> >> 列表<list<long> > 使用 java 流</list<long></list<map.entry<long,> - How to convert Collection<List<Map.Entry<Long, Long>>> to List<List<Long>> using java streams 使用流将自定义对象列表合并到单个列表 Object Java 8 - Merge List of Custom Objects to a Single List Object using Streams Java 8 Java 8 Streams - 使用流将相同类型的多个对象映射到列表 - Java 8 Streams - Map Multiple Object of same type to a list using streams 转换清单 <E> 到地图 <String, List<String> &gt;使用Java 8流 - convert List<E> to Map<String, List<String>> using java 8 streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM