作者:摩羯法国反弹 | 来源:互联网 | 2022-12-07 19:55
您需要这样的东西:
List firstlist = new ArrayList<>();
firstlist.add(new ObjectA(3, Arrays.asList("test", "test2")));
List secOndList= new ArrayList<>();
secondList.add(new ObjectB("test", 3));
List result = firstlist.stream()
.flatMap(a -> secondList.stream()
.filter(b -> a.getNames().contains(b.getName()))
.map(c -> new ObjectC(a.getId(), c.getName(), c.getAge()))
).collect(Collectors.toList());
如果我理解您的问题,您将有三个不同的对象,如下所示:
@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class ObjectA {
private int id;
private List names;
}
@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class ObjectB {
private String name;
private int age;
}
//And the result Object you want to get
@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
public class ObjectC {
private int id;
private String name;
private int age;
}
此示例的输出是:
[ObjectC(id=3, name=test, age=3)]
对于注释,我正在使用Lombok
1> YCF_L..:
您需要这样的东西:
List firstlist = new ArrayList<>();
firstlist.add(new ObjectA(3, Arrays.asList("test", "test2")));
List secOndList= new ArrayList<>();
secondList.add(new ObjectB("test", 3));
List result = firstlist.stream()
.flatMap(a -> secondList.stream()
.filter(b -> a.getNames().contains(b.getName()))
.map(c -> new ObjectC(a.getId(), c.getName(), c.getAge()))
).collect(Collectors.toList());
如果我理解您的问题,您将有三个不同的对象,如下所示:
@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class ObjectA {
private int id;
private List names;
}
@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class ObjectB {
private String name;
private int age;
}
//And the result Object you want to get
@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
public class ObjectC {
private int id;
private String name;
private int age;
}
此示例的输出是:
[ObjectC(id=3, name=test, age=3)]
对于注释,我正在使用Lombok