1、流去重
list.stream()l.collect(Collectors.toMap(User::getName, v -> v, (v1, v2) -> v1))
==============================================================================
list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(InventoryTemplate::getCode))), ArrayList::new));
=======================================================================================
Map<String, String> res = list.stream().collect(Collectors.groupingBy(Dao::getSkuId, Collectors.collectingAndThen(Collectors.toList(), list -> list.get(0).getObjectId() + getSplitFlag() + list.get(0).getId())));
inventories.stream().distinct()
.filter(item->ObjectUtils.isNotEmpty(item.getTargetId()) && ObjectUtils.isNotEmpty(item.getGoodsLocationId()))
.collect(Collectors.groupingBy(Inventory::getTargetId,
Collectors.mapping(Inventory::getGoodsLocationId, Collectors.toList())));
2、流汇总计数
list.stream().collect(Collectors.groupingBy(BubblesVo::getType, Collectors.counting()));
3、流汇总求和
list.stream().collect(Collectors.groupingBy(BubblesVo::getType, Collectors.summingInt(BubblesVo::getNum)));
4、限流
CountDownLatch countDownLatch = new CountDownLatch(templateListList.size());
String rateLimiterName = "rateLimiter:importInventory:" + UserContext.get().getCompanyId();
templateListList.forEach(templateList -> {
new Thread(() -> {
try {
do();
} catch (Exception e) {
reDo();
} finally {
countDownLatch.countDown();
}
}).start();
//预防OOM,最大流速 = 每1秒钟产生5个令牌[1秒5次请求]
RRateLimiter downloadLimiter = redisson.getRateLimiter(rateLimiterName);
downloadLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.SECONDS);
downloadLimiter.acquire();
});
5、list排序
升序
list.sort(Comparator.comparing(T::getSort).thenComparing(T::getCreatTime));
先升后降
list.sort(Comparator.comparing(T::getSort).thenComparing(T::getCreateTime).reversed());