简体   繁体   English

为什么BitSet不可以进行?

[英]Why is BitSet not Iterable?

BitSet has a stream() method but it does not implement the Iterable interface like other types that provide this method. BitSet有一个stream()方法,但它没有像提供此方法的其他类型那样实现Iterable接口。 Is there a specific reason for this? 这有什么特别的原因吗?

None of the methods in Iterable ( foreach , iterator , and spliterator ) is provided in BitSet . BitSet没有提供Iterableforeachiteratorspliterator )中的任何BitSet There is no stream() method in Iterable . Iterable没有stream()方法。

Furthermore the stream() method of BitSet does not return a stream over the bits of the bit set, but returns a stream over the indices of the bits whose values are set (which is kind of confusing TBH). 此外, BitSetstream()方法不会在位集的位上返回流,而是在其值设置的位索引上返回流(这是令人困惑的TBH)。 Therefore, technically speaking there seems to be almost nothing in common with Iterable . 因此,从技术上讲,似乎与Iterable几乎没有任何共同之处。

One reason (not the entire reason, maybe) is that Iterable would inefficient, because the bit indexes have to be boxed (*); 一个原因(可能不是全部原因)是Iterable效率低下,因为比特索引必须加框(*); the stream is able to use primitive ints. 流可以使用原始int。

There's an efficient way to iterate the bitset without using Iterable , as described in the Javadoc, so it's not really necessary. 有一种有效的方法可以在不使用Iterable情况下迭代bitset,如Javadoc中所述,所以它并不是必需的。


(*) However, for bitsets with size 128 or smaller, boxing would be cheap, as cached boxed instances would be used. (*)但是,对于大小为128或更小的位集,装箱会很便宜,因为将使用缓存的盒装实例。

BitSet is not a "true" member of the java collection framework, so technically, no need to implement Collection.iterator() and provide one. BitSet不是java集合框架的“真正”成员,因此从技术上讲,不需要实现Collection.iterator()并提供一个。

public class BitSet implements Cloneable, java.io.Serializable 

More to the point, both would be ill-fitted togethoer. 更重要的是,两者都是不合适的。

BitSet are not generic , unlike java.util.Iterator; 与java.util.Iterator不同,BitSet不是通用的 ; BitSet provides ad-hoc methods with special features for side-effects and random addressing, unlike Iterator. 与Iterator不同,BitSet为ad-hoc方法提供了副作用和随机寻址的特殊功能。

Probably to avoid expensively boxing every bit to a Boolean instance. 可能要避免昂贵地将每一位装Boolean实例。

Looping over it using its own APIs will avoid all allocations. 使用自己的API循环使用它将避免所有分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM