简体   繁体   English

Java 8流:将ArrayLists中的两个对象的属性组合为第三种对象类型的ArrayList

[英]Java 8 streams : Combine properties of two objects which are in ArrayLists into an ArrayList of a third object type

I'm new to the Java 8 streams and would appreciate some help in learning. 我是Java 8流的新手,希望能对学习有所帮助。

I have an Arraylist of User objects and and Arraylist of UserCompany objects. 我有一个User对象的Arraylist和UserCompany对象的Arraylist。 The User object has a user_id and associated user information The UserCompany list has the user's Company object but only has the user_id of the User. User对象具有user_id和关联的用户信息。UserCompany列表具有用户的Company对象,但仅具有User的user_id。 I would like to create a third object called UserCompanyView that is a combination of the User object and the Company object using Java 8 streams. 我想使用Java 8流创建一个名为UserCompanyView的第三个对象,该对象是User对象和Company对象的组合。 I have only been able to find examples of two arrays being concatenated or merged , like,: 我只能找到两个被串联或合并的数组的示例,例如:

 Stream.of(list1, list2)
.flatMap(x -> x.stream())
.collect(Collectors.toList());

but nothing where specific properties of the individual lists are used to create a third Object. 但是没有使用单个列表的特定属性创建第三个对象的地方。

the code should : 该代码应:

1) iterate through the UserCompany list 1)遍历UserCompany列表

2)Check if the UserCompany user_id matches the User list user_id 2)检查UserCompany user_id是否与用户列表user_id相匹配

3) if 2 is true , create a UserCompanyView object using the User and the UserCompany 3)如果2为true,则使用User和UserCompany创建一个UserCompanyView对象

4) Add the UserCompanyView from 3 to a new List and return it. 4)将UserCompanyView从3添加到新列表并返回它。

Thanks for viewing this post and taking time to reply 感谢您查看这篇文章并花时间回复

In order for it to perform, you start by building a Map of user_id to User object. 为了使其执行,首先要构建一个user_idUser对象的Map

Using streams, you'd do it like this: 使用流,您可以这样做:

List<User> users = // built elsewhere

Map<Integer, User> userById = users.stream()
        .collect(Collectors.toMap(User::getUserId, u -> u));

Then you iterate and UserCompany objects, lookup the User object, and create the UserCompanyView object, adding them to a List . 然后,迭代和UserCompany对象,查找User对象,并创建UserCompanyView对象,然后将它们添加到List

Using streams, you'd do it like this: 使用流,您可以这样做:

List<UserCompany> userCompanies = // built elsewhere

List<UserCompanyView> views = userCompanies.stream()
        .map(uc -> new UserCompanyView(userById.get(uc.getUserId()), uc))
        .collect(Collectors.toList());

If they don't follow the same order, you'll need to create an ID map first: 如果他们没有遵循相同的顺序,则需要首先创建一个ID映射:

Map<Integer, User> usersById = users.stream()
        .collect(Collectors.toMap(User::getUserId, u -> u));

Now you can stream the other list and map each element to its matching User by ID: 现在,您可以流式传输其他列表,并将每个元素按ID映射到与其匹配的User

List<UserCompanyView> views = userCompanies.stream()
        .map(uc -> new UserCompanyView(usersById.get(uc.getUserId()), uc))
        .collect(Collectors.toList())

If there are UserCompany s without matching User s, you can filter them out by adding this before map() : 如果存在UserCompany而不匹配的User ,则可以通过在map()之前添加以下内容来过滤掉它们:

.filter(uc -> usersById.containsKey(uc.getUserId()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM