我正在阅读 java 8 Random 中的新增强功能类并且重复使用了这个术语有效无限流。
考虑 IntStream ints(int randomNumberOrigin, int randomNumberBound) :
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
谁能解释一下这个术语。
最佳答案
这意味着您可以将其视为无限制,但从技术上讲它可能不是无限制的。例如,openjdk 8u40-b25 implementation返回 Long.MAX_VALUE
元素流:
public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
if (randomNumberOrigin >= randomNumberBound)
throw new IllegalArgumentException(BadRange);
return StreamSupport.intStream
(new RandomIntsSpliterator
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
false);
}
您看到它调用了 new RandomIntsSpliterator (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
。
RandomIntsSpliterator
的 Javadoc 说:
Spliterator for int streams. We multiplex the four int versions into one class by treating a bound less than origin as unbounded, and also by treating "infinite" as equivalent to Long.MAX_VALUE. For splits, it uses the standard divide-by-two approach. The long and double versions of this class are identical except for types.
这意味着这里的“有效无限流”被实现为具有大量元素(其中的Long.MAX_VALUE
)的实际有限流。出于所有实际目的,我想它可以被视为无限流,因为任何人都希望使用这种方法产生超过 Long.MAX_VALUE
随机 int
的机会是可能很小。
如果您查看返回请求大小的 IntStream
的 ints
的类似变体:
public IntStream ints(long streamSize, int randomNumberOrigin,
int randomNumberBound) {
if (streamSize < 0L)
throw new IllegalArgumentException(BadSize);
if (randomNumberOrigin >= randomNumberBound)
throw new IllegalArgumentException(BadRange);
return StreamSupport.intStream
(new RandomIntsSpliterator
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
false);
}
你看到那个电话了
ints(min,max)
相当于调用
ints(Long.MAX_VALUE,min,max)
至少在这个实现中是这样。
关于java - "effectively unlimited stream"是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47567052/