英文:
How to iterate over several lists of objects and get summary of objects in Java?
问题
假设我有一个如下的类。
public class User {
private String userType;
private int numOfUsers;
}
而且有不止一个包含User类型对象的列表。
List<User> list1 = < (userType:clerk, numofUsers:3 ), (userType:painter, numofUsers:4 ), (userType:engineer, numofUsers:10 ) >;
List<User> list2 = < (userType:clerk, numofUsers:3 ),(userType:electrician, numofUsers:14 ), (userType:welder, numofUsers:5 ), (userType:engineer, numofUsers:10) >;
List<User> list3 = < (userType:carpenter, numofUsers:4 ),(userType:welder, numofUsers:10 ) >;
我需要创建一个包含用户总数的单一列表,其格式如下。
List<User> userCount = < (userType:clerk, numofUsers:6 ), (userType:painter, numofUsers:4 ), (userType:engineer, numofUsers:20 ),(userType:electrician, numofUsers:14 ), (userType:carpenter, numofUsers:4 ) ,(userType:welder, numofUsers:15 ) >;
在Java中我该如何做到这一点?
英文:
Suppose I have a class as follows.
public class User {
private String userType;
private int numOfUsers;
}
And there are more than one list which consists objects of User type.
List<User> list1 = < (userType:clerk, numofUsers:3 ), (userType:painter, numofUsers:4 ), (userType:engineer, numofUsers:10 ) >
List<User> list2 = < (userType:clerk, numofUsers:3 ),(userType:electrician, numofUsers:14 ), (userType:welder, numofUsers:5 ), (userType:engineer, numofUsers:10) >
List<User> list3 = < (userType:carpenter, numofUsers:4 ),(userType:welder, numofUsers:10 ) >
I need to create single list of users which gives the summation of every types of user objects as follows.
List<User> userCount = < (userType:clerk, numofUsers:6 ), (userType:painter, numofUsers:4 ), (userType:engineer, numofUsers:20 ),(userType:electrician, numofUsers:14 ), (userType:carpenter, numofUsers:4 ) ,(userType:welder, numofUsers:15 ) >
How can i do that in Java?
答案1
得分: 3
你是否希望使用:
List<User> result = Stream.of(list1, list2, list3)
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(User::getUserType, Collectors.summingInt(User::getNumOfUsers)))
.entrySet().stream()
.map(e -> new User(e.getKey(), e.getValue()))
.collect(Collectors.toList());
英文:
Are you looking to use :
List<User> result = Stream.of(list1, list2, list3)
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(User::getUserType, Collectors.summingInt(User::getNumOfUsers)))
.entrySet().stream()
.map(e -> new User(e.getKey(), e.getValue()))
.collect(Collectors.toList());
答案2
得分: 1
你可以使用Java Stream API。使用Collectors.toMap
并合并相同类型的NumOfUsers
。然后将值放入ArrayList中。
List<User> result = new ArrayList<>(
Stream.of(list1, list2, list3) // List of List的Stream
.flatMap(Collection::stream) // 扁平化为单个List
.collect(Collectors.toMap(
User::getUserType, // 以UserType为键
i -> i, // 值为元素本身
(a, b) -> new User(a.getUserType(), a.getNumOfUsers() + b.getNumOfUsers())
// 合并具有相同类型的用户
))
.values()); // 获取Map中的值,放入ArrayList中
英文:
You can use Java Stream API. Use Collectors.toMap
and merge NumOfUsers
for same type. Then get the values in ArrayList.
List<User> result = new ArrayList<>(
Stream.of(list1, list2, list3) // Stream of List of List
.flatMap(Collection::stream) // Flatten into a single list
.collect(Collectors.toMap(User::getUserType, i -> i, //Map by UserType and merge user having same type
(a,b) -> new User(a.getUserType(), a.getNumOfUsers() + b.getNumOfUsers())))
.values()); // Take only values of the map
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论