[英]Java 8 Streams - Nested Maps to List
firstlist
.stream()
.map( x -> {
return secondList
.stream()
.map( y -> { //return a string } )
.collect(Collectors.toList()) // "Output" I need
}
)
.//Get the "Output" here
我有两个清单。 第一个列表中的项目必须与第二个列表进行比较,并且必须建立新的列表。
样本输入
List 1 : [ { "id" ; 3, "names" : ["test","test2"] }]
List 2 : [ {"name": :"test" , "age" :3}]
输出:
List 3 : [ {"id" : 3, "name" : "test", "age" :3} ]
PS:应对照第二个列表检查第一个列表中的names
您需要这样的东西:
List<ObjectA> firstlist = new ArrayList<>();
firstlist.add(new ObjectA(3, Arrays.asList("test", "test2")));
List<ObjectB> secondList = new ArrayList<>();
secondList.add(new ObjectB("test", 3));
List<ObjectC> 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<String> 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
是您要找的东西吗?
firstList
.stream()
.filter(item -> secondList
.stream().anyMatch(item::matches))
.collect(Collectors.toList());
您的属性是如此通用,我认为您最好引入一些自定义类来替换map
并满足您的要求,这是问题中输入和输出的简单演示:
public class SimpleFlatMap {
public static void main(String... args) {
List<EntityWithNames> listOne = new ArrayList<>();
listOne.add(new EntityWithNames());
List<EntityWithAge> listTwo = new ArrayList<>();
listTwo.add(new EntityWithAge("test", 3));
List<EntityWithNameAndAge> listThree = listOne.stream().map(withNames -> {
EntityWithAge entityWithAge = listTwo.stream()
.filter(withAge -> withNames.names.contains(withAge.name))
.findAny()
.orElse(new EntityWithAge());
return new EntityWithNameAndAge(withNames.id, entityWithAge.name, entityWithAge.age);
}).collect(Collectors.toList());
System.out.println(listThree);
}
static class EntityWithNames {
Long id;
List<String> names;
public EntityWithNames() {
id = 3L;
names = new ArrayList<>(Arrays.asList("test", "test1"));
}
}
static class EntityWithAge {
String name;
int age;
public EntityWithAge() {
name = "default";
age = -1;
}
public EntityWithAge(String name, int age) {
this.name = name;
this.age = age;
}
}
static class EntityWithNameAndAge {
Long id;
String name;
int age;
public EntityWithNameAndAge(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("id: %d, name: %s, age: %d", id, name, age);
}
}
}
输出:
[id: 3, name: test, age: 3]
您可能需要以下内容:
List<ClassWithId> list1 = new ArrayList<>();
List<ClassWithAge> list2 = new ArrayList<>();
list1.add(new ClassWithId(3, Arrays.asList("test", "test2")));
list2.add(new ClassWithAge("test", 4));
List<ClassResult> list3 = list2.stream()
.map(i -> new ClassResult(
list1.stream()
.filter(j -> j.getList().contains(i.getName()))
.map(j -> j.getId())
.findFirst().get(),
i.getName(), i.getAge()))
.collect(Collectors.toList());
此解决方案假定以下两个输入对象和输出对象的结构:
ClassWithId
private int id; private List<String> list;
ClassWithAge
private String name; private int age;
类结果
private int id; private int age; private String name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.