简体   繁体   English

GroupingBy 在 java 流 API

[英]GroupingBy in java streams API

input:输入:

在此处输入图像描述

OutPut: SessionId TimeSpent OutPut:SessionId TimeSpent
123 15 123 15
124 2 124 2

i have to calculate total time spent for each session.我必须计算每个 session 花费的总时间。 I have tried below code for getting above output.我已经尝试了下面的代码来获得高于 output 的代码。

List<IsolatedTrafficPerSession> l  = new ArrayList();
        Map<String,Map<String,List<IsolatedDetailQueryResponseDTO>>> mapg = list.stream().collect(Collectors.groupingBy(IsolatedDetailQueryResponseDTO::getIsolateSessionId, Collectors.groupingBy(IsolatedDetailQueryResponseDTO::getIsolateTabId)));
        for(String sessionid: mapg.keySet()){
            int timeSpent  = 0;
            Map<String,List<IsolatedDetailQueryResponseDTO>> m = mapg.get(sessionid);
             for(String tabId: m.keySet()){
                 for(IsolatedDetailQueryResponseDTO isolatedDetailQueryResponseDTO: m.get(tabId)){
                     if(isolatedDetailQueryResponseDTO.getIsolateType() == 0) {
                         timeSpent = timeSpent+ isolatedDetailQueryResponseDTO.getCreatedTimestamp();
                     }
                    
                 }

             }
            IsolatedTrafficPerSession isolatedTrafficPerSession = new IsolatedTrafficPerSession();
             isolatedTrafficPerSession.setSession_id(sessionid);
             isolatedTrafficPerSession.setTimeSpent(timeSpent);
             l.add(isolatedTrafficPerSession);

        }`

But My code looks like it's taking O(n^3) time complexity.但是我的代码看起来需要 O(n^3) 时间复杂度。 Is there any better solution to get above my out put.有没有更好的解决方案来超越我的输出。

For you scenario, GroupingBy may be redundant.对于您的情况, GroupingBy可能是多余的。 You can just collect the dtos to a Map and return the values collection.您可以将Map返回values集合。

Code Snippet:代码片段:

public static Collection<IsolatedTrafficPerSession> groupBasedOnSessionId(List<IsolatedDetailQueryResponseDTO> list) {
    return list.stream()
               .collect(Collectors.toMap(
                       IsolatedDetailQueryResponseDTO::getIsolateSessionId,
                       dto -> new IsolatedTrafficPerSession()
                               .setSession_id(dto.getIsolateSessionId())
                               .setTimeSpent(dto.getIsolateType() == 0 ? dto.createdTimestamp : 0),
                       ( v1, v2) -> v1.setTimeSpent(v1.getTimeSpent() + v2.getTimeSpent())))
               .values();
}

Test:测试:

public static void main(String[] args) {
    List<IsolatedDetailQueryResponseDTO> input = Arrays.asList(
            new IsolatedDetailQueryResponseDTO("123", "0", 1, 0),
            new IsolatedDetailQueryResponseDTO("123", "1", 2, 0),
            new IsolatedDetailQueryResponseDTO("124", "0", 2, 0),
            new IsolatedDetailQueryResponseDTO("123", "0", 0, 10),
            new IsolatedDetailQueryResponseDTO("123", "1", 0, 5),
            new IsolatedDetailQueryResponseDTO("124", "0", 0, 2));

    Collection<IsolatedTrafficPerSession> result = groupBasedOnSessionId(input);
    result.forEach(System.out::println);
}

Output: Output:

123 15
124 2

Note:笔记:

  1. Used fluent style setter for IsolatedTrafficPerSession as below.使用了如下的IsolatedTrafficPerSession的流利样式设置器。
     public IsolatedTrafficPerSession setSession_id(String sessionId) { this.sessionId = sessionId; return this; }
  2. Constructor used for IsolatedDetailQueryResponseDTO :用于IsolatedDetailQueryResponseDTO的构造函数:
     public IsolatedDetailQueryResponseDTO(String isolateSessionId, String isolateTabId, int isolateType, int createdTimestamp) { this.isolateSessionId = isolateSessionId; this.isolateTabId = isolateTabId; this.isolateType = isolateType; this.createdTimestamp = createdTimestamp; }

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

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