[英]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.注意:类
X
、 Y
和Z
之间没有关系,也没有公共接口。
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>
.由于您的
X
、 Y
和Z
类型没有共同的超类型,因此您必须将它们转换为一些共同的类型,例如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.