gpt4 book ai didi

java - 从流中获取最后 n 个元素

转载 作者:IT老高 更新时间:2023-10-28 21:12:11 25 4
gpt4 key购买 nike

我想知道有没有替代方法

List<X> lastN = all.subList(Math.max(0, all.size() - n), all.size());

stream用法?

最佳答案

使用 Stream.skip()

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.

all.stream().skip(Math.max(0, all.size() - n)).forEach(doSomething);

关于java - 从流中获取最后 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30476127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com