您当前的位置: 首页 >  ide

IDEA 告警:Stream API call chain can be simplified

发布时间:2020-10-25 23:00:12 ,浏览量:0

通知流 API 调用链,可以简化。它允许在遍历集合时避免创建冗余的临时对象。

此检查替换了以下调用链:

collection.stream().forEach() → collection.forEach() collection.stream().collect(toList/toSet/toCollection()) → new CollectionType<>(collection) collection.stream().toArray() → collection.toArray() Arrays.asList().stream() → Arrays.stream() or Stream.of() IntStream.range(0, array.length).mapToObj(idx -> array[idx]) → Arrays.stream(array) IntStream.range(0, list.size()).mapToObj(idx -> list.get(idx)) → list.stream() Collections.singleton().stream() → Stream.of() Collections.emptyList().stream() → Stream.empty() stream.filter().findFirst().isPresent() → stream.anyMatch() stream.collect(counting()) → stream.count() stream.collect(maxBy()) → stream.max() stream.collect(mapping()) → stream.map().collect() stream.collect(reducing()) → stream.reduce() stream.collect(summingInt()) → stream.mapToInt().sum() stream.mapToObj(x -> x) → stream.boxed() stream.map(x -> {...; return x;}) → stream.peek(x -> ...) !stream.anyMatch() → stream.noneMatch() !stream.anyMatch(x -> !(...)) → stream.allMatch() stream.map().anyMatch(Boolean::booleanValue) -> stream.anyMatch() IntStream.range(expr1, expr2).mapToObj(x -> array[x]) -> Arrays.stream(array, expr1, expr2) Collection.nCopies(count, ...) -> Stream.generate().limit(count) stream.sorted(comparator).findFirst() -> Stream.min(comparator) 

请注意,替换语义在某些情况下可能会有细微的差别。例如,当Collections.synchronizedList(…)。forEach()同步时,Collections.synchronizedList(…)。stream()。forEach()不同步。否则,如果结果元素为null,则collect(Collectors.maxBy())将返回空的Optional,而在这种情况下Stream.max()将抛出NullPointerException。

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0453s