输入:
输出:sessionid timespent
123 15
124 2
我必须计算每节课的总时间。我已经尝试了下面的代码来获得上面的输出。
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);
}`
但是我的代码看起来需要o(n^3)时间复杂度。有没有更好的办法来克服我的困难。
1条答案
按热度按时间ovfsdjhp1#
对你来说,
GroupingBy
可能是多余的。您只需将DTO收集到Map
并归还values
收藏。代码段:
测试:
输出:
注:
使用流畅的样式设置器
IsolatedTrafficPerSession
如下所示。用于
IsolatedDetailQueryResponseDTO
: