【发布时间】:2017-05-16 13:56:44
【问题描述】:
我有过滤器personCountFilter=3,列表如下:
Rate{ PersonCount:1, LOS:1}
Rate{ PersonCount:1, LOS:2}
Rate{ PersonCount:1, LOS:3}
Rate{ PersonCount:2, LOS:1}
Rate{ PersonCount:2, LOS:2}
Rate{ PersonCount:2, LOS:3}
Rate{ PersonCount:3, LOS:2}
Rate{ PersonCount:3, LOS:4}
Rate{ PersonCount:3, LOS:5}
Rate{ PersonCount:3, LOS:6}
Rate{ PersonCount:4, LOS:3}
Rate{ PersonCount:5, LOS:7}
Rate{ PersonCount:6, LOS:7}
过滤后我的预期:
Rate{ PersonCount:2, LOS:1}
Rate{ PersonCount:3, LOS:2}
Rate{ PersonCount:4, LOS:3}
Rate{ PersonCount:3, LOS:4}
Rate{ PersonCount:3, LOS:5}
Rate{ PersonCount:3, LOS:6}
Rate{ PersonCount:5, LOS:7}
按LOS分组后如何获取值,如果personCount匹配过滤器得到这个,如果不匹配,则最接近personCountFilter,首先是更大的personCountFilter
我尝试使用
HashSet<Rate> testSet = rates.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(Rate::getLengthOfStayCount,
Function.identity(),
(previous, current) ->
{
return previous.getPersonCount() >
current.getPersonCount() ? previous : current;
}),
map ->
{
HashSet<Rate> set = new HashSet<>();
set.addAll(map.values());
return set;
}));
但它会返回
Rate{ PersonCount:2, LOS:1}
Rate{ PersonCount:3, LOS:2}
Rate{ PersonCount:4, LOS:3}
Rate{ PersonCount:3, LOS:4}
Rate{ PersonCount:3, LOS:5}
Rate{ PersonCount:3, LOS:6}
Rate{ PersonCount:6, LOS:7}
当前按 LOS 分组后获得最大 personCount
【问题讨论】:
-
你能把问题说得更清楚吗.. 我不明白预期的内容:确切地说,我不明白“如果 personCount 匹配过滤器得到这个,如果不匹配,与 personCountFilter 最接近,更大的 personCountFilter 优先”
标签: java java-8 java-stream