如何在Java-8中正确查找流特性? [英] How to correctly find the stream characteristics in Java-8?
问题描述
在进行流操作时,在中间/ pleleline操作期间,将创建具有不同特征的流(例如SORTED / SIZED / DISTINCT / ORDERED) - 掌握Lambdas(第6章)
While doing stream operations, during the intermediate/pipleline operations the streams would be created with different characteristics(e.g SORTED/SIZED/DISTINCT/ORDERED) - Mastering Lambdas(Ch 6)
Stream.of(8,3,5,6,7,4)//ORDERED, SIZED
.filer(i->i%2==0) // ORDERED
.sorted() // ORDERED, SORTED
.distinct() // DISTINCT, ORDERED, SORTED
.map(i->i+1) // ORDERED
.unordered(); //none
我们如何找出上述代码段中提到的流的不同特征?
How do we find out the different characteristics of the stream as mentioned in the above snippet?
推荐答案
我想稍微扩展一下assylias所说的内容(绝对正确)。
I would like to slightly extend what assylias said (which is absolutely correct).
首先,这些特性实现为普通的 int
,它是二进制表示。首先它是全零,但当你添加某个特征时,它的位被设置为一个
通过 OR
操作,删除通过 AND
操作。
First, these characteristics are implemented as plain int
, it's binary representation. First it's all zeroes, but when you add a certain characteristic it's bit is set to one
via the OR
operation, removed via the AND
operation.
您可以通过以下方式查看某个Spliterator属性设置其一个
的位置:
You can see where a certain Spliterator property sets its one
simply by doing this for example:
System.out.println(Integer.toBinaryString(Spliterator.SIZED)); // 1000000
它将第7位设置为右边的一位。因此,当您检查:
It's setting the 7-th bit into one from the right. So when you check:
Spliterator<Integer> spliterator = Stream.of(8, 3, 5, 6, 7, 4).spliterator();
System.out.println((spliterator.characteristics() & Spliterator.SIZED) == Spliterator.SIZED);
您实际上正在检查此特定位是否已设置。
You are actually checking if this particular bit is set.
第二
4 流特征设置为第一个流创建的结果(而不是两个)。这本书有点过时或你没有向我们展示整个例子:
There are 4 stream characteristics that are set as the result of your first stream creation(and not two). Either the book is a bit outdated or you have not showed us the entire example:
Spliterator<Integer> spliterator = Stream.of(8, 3, 5, 6, 7, 4).spliterator();
System.out.println(Integer.bitCount(spliterator.characteristics())); // 4
System.out.println(Integer.toBinaryString(spliterator.characteristics()));// 100010001010000
这些设置位(等于一个
)对应 SIZED
, ORDERED
, IMMUTABLE
, SUBSIZED
。
These set bits (that are equal to one
) correspond to SIZED
, ORDERED
, IMMUTABLE
, SUBSIZED
.
您展示的其他人显然也略显偏差 - 您可以自行查看。
The others that you have shown are obviously slightly off too - you can check those yourself.
第三次
这些特性在流处理中非常重要。一些例子:
These characteristics are extremely important in stream processing. A few examples:
long howMany = Stream.of(1, 2, 3).map(x -> {
System.out.println("mapping");
return x * 2;
}).count();
System.out.println(howMany); // 3
在java-9中你不会看到的映射
打印,因为你没有更改流(你还没有清除 SIZED
特征);因此根本不需要甚至评估映射。
In java-9 you will not see the mapping
printed, because you have not changed the stream (you have not cleared the SIZED
characteristic); thus no need to even evaluate the mapping at all.
Stream<Integer> unlimited = Stream.iterate(0, x -> x + 1);
System.out.println(unlimited.spliterator().hasCharacteristics(Spliterator.SIZED));
Stream<Integer> limited = unlimited.limit(3);
System.out.println(limited.spliterator().hasCharacteristics(Spliterator.SIZED));
你会认为输出应该是 false true
- 毕竟我们正在添加限制
,但是没有;结果是 false false
:没有进行这样的优化,即使没有太多阻止它。
You would think that the output should be false true
- we are adding a limit
after all, but no; the result is false false
: no such optimization is done, even if there is not much preventing it.
这篇关于如何在Java-8中正确查找流特性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!