如何在Java中迭代遍历多个对象列表并获取对象摘要?

huangapple go评论109阅读模式
英文:

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&lt;User&gt; list1 = &lt; (userType:clerk, numofUsers:3 ), (userType:painter, numofUsers:4 ), (userType:engineer, numofUsers:10 ) &gt;
        
List&lt;User&gt; list2 = &lt; (userType:clerk, numofUsers:3 ),(userType:electrician, numofUsers:14 ), (userType:welder, numofUsers:5 ), (userType:engineer, numofUsers:10) &gt;
    
List&lt;User&gt; list3 = &lt; (userType:carpenter, numofUsers:4 ),(userType:welder, numofUsers:10 ) &gt;

I need to create single list of users which gives the summation of every types of user objects as follows.

List&lt;User&gt; userCount  = &lt; (userType:clerk, numofUsers:6 ), (userType:painter, numofUsers:4 ), (userType:engineer, numofUsers:20 ),(userType:electrician, numofUsers:14 ), (userType:carpenter, numofUsers:4 ) ,(userType:welder, numofUsers:15 ) &gt;

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&lt;User&gt; result = Stream.of(list1, list2, list3)
        .flatMap(Collection::stream)
        .collect(Collectors.groupingBy(User::getUserType, Collectors.summingInt(User::getNumOfUsers)))
        .entrySet().stream()
        .map(e -&gt; 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&lt;User&gt; result = new ArrayList&lt;&gt;(
       Stream.of(list1, list2, list3)  // Stream of List of List
        .flatMap(Collection::stream)   // Flatten into a single list
        .collect(Collectors.toMap(User::getUserType, i -&gt; i,   //Map by UserType and  merge user having same type
                 (a,b) -&gt; new User(a.getUserType(), a.getNumOfUsers() + b.getNumOfUsers())))
        .values());   // Take only values of the map

huangapple
  • 本文由 发表于 2020年8月19日 15:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63481836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定