通过匹配其他两个列表中的条目来创建新的Java对象列表

假设我有这两个清单


List<Person> persons = Arrays.asList(

                new Person(1, "Mike", "Canada"),

                new Person(2, "Jill", "England"),

                new Person(3, "Will", "Whales"),

                new Person(4, "Mary", "Spain"));



List<Metadata> metadata= Arrays.asList(

                new metadata(1, "2000-01-01", "Naturalized", "Bachelor's of Arts"),

                new metadata(2, "2001-01-01", "ExPat", "Masters of Chemestry"),

                new metadata(3, "2017-05-01", "Citizen", "Buiness Management"),

                new metadata(4, "2018-04-16", "Work Visa", "Nursing"));

最终结果是一个新列表:


List<PersonWithMetadata> personsAndMEtadata = Arrays.asList(

                new PersonWithMetadata(1, "Mike", "Canada", "2000-01-01", "Naturalized", "Bachelor's of Arts"),

                new PersonWithMetadata(2, "Jill", "England", "2001-01-01", "ExPat", "Masters of Chemestry"),

                new PersonWithMetadata(3, "Will", "Whales", "2017-05-01", "Citizen", "Buiness Management"),

                new PersonWithMetadata(4, "Mary", "Spain", "2018-04-16", "Work Visa", "Nursing"));

我正在尝试找到一种将前两个列表组合成第三个列表的Java流方法-就像第一个输入上的SQL连接是一个ID号一样。似乎应该有一种方法可以做到这一点,但是我很茫然。这是怎么做的?另外,假设两个输入列表之间最多有一个匹配项。


跃然一笑
浏览 111回答 3
3回答

弑天下

YCF_L的解决方案应该可以使用,但是它是O(n 2)解决方案。O(n)解决方案可以通过将一个列表转换为从id到对象的映射,然后在另一个列表上进行迭代并从映射中获取匹配值来实现:Map<Integer, Person> personMap =&nbsp;&nbsp; &nbsp; persons.stream().collect(Collectors.toMap(Person::getId, Function.identity());List<PersonWithMetadata> result =&nbsp;&nbsp; &nbsp; metadata.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(m -> new PersonWithMetadata(personMap.get(m.getId()), m)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());在示例数据中,列表具有匹配顺序的匹配对象。如果此假设也适用于实际问题,则解决方案可能必须更简单-您可以流式传输索引并从列表中获取相应的值:List<PersonWithMetadata> result =&nbsp;&nbsp; &nbsp; IntStream.reange(0, persons.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(i -> new PersonWithMetadata(persons.get(i), metadata.get(i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());

幕布斯6054654

您可以这样尝试:List<PersonWithMetadata> personsAndMEtadata = persons.stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(p -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //search for the meta data based on the person id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Metadata meta = metadata.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(m -> m.getId() == p.getId())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // then create a PersonWithMetadata object based on Person and metadata&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new PersonWithMetadata(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.getId(), p.getFirstName(), p.getLastName(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meta.getDate(), meta.getCity(), meta.getJob()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ).collect(Collectors.toList());关于这条线:Metadata meta = metadata.stream().filter(m -> m.getId() == p.getId()).findFirst().get();我假设您有一个id为person的元数据,否则您将得到NullPointerException。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java