Java 8 Streams - 要列表的嵌套映射

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应根据第二个列表检查第一个列表中的内容


米琪卡哇伊
浏览 151回答 3
3回答

RISEBY

你需要这样的东西: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()&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(a -> secondList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(b -> a.getNames().contains(b.getName()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(c -> new ObjectC(a.getId(), c.getName(), c.getAge()))&nbsp; &nbsp; &nbsp; &nbsp; ).collect(Collectors.toList());如果我理解你的问题,你有三个不同的对象,如下所示:@Getter @Setter @AllArgsConstructor @NoArgsConstructorpublic class ObjectA {&nbsp; &nbsp; private int id;&nbsp; &nbsp; private List<String> names;}@Getter @Setter @AllArgsConstructor @NoArgsConstructorpublic class ObjectB {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int age;}//And the result Object you want to get@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToStringpublic class ObjectC {&nbsp; &nbsp; private int id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int age;}这个例子的输出是:[ObjectC(id=3, name=test, age=3)]对于注释,我使用 Lombok

LEATH

您可能需要以下内容:List<ClassWithId>&nbsp; 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()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(i -> new ClassResult(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list1.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(j -> j.getList().contains(i.getName()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(j -> j.getId())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst().get(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i.getName(), i.getAge()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());此解决方案假定以下两个输入对象和输出对象的结构:带标识的类private int id;private List<String> list;年龄分类private String name;private int age;类结果private int id;private int age;private String name;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java