如果工作对于人类不是人生强索的代价,而是目的,人类将是多么幸福——罗丹
我们操作集合同样可以使用Spliterator
Stream
子类ReferencePipeline
的构造函数中需要传入Spliterator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| List<Integer> list = Stream.iterate(0, i -> i < 100, i -> ++i).toList();
Spliterator<Integer> spliterator = list.spliterator();
Spliterator<String> spliterator1 = Spliterators.spliterator(Collections.singleton("xxx"), 0);
boolean b = spliterator.tryAdvance(x -> System.out.println("消费第一个元素:" + x)); System.out.println("是否还有下一个:" + b); spliterator.tryAdvance(x -> System.out.println("消费第二个元素:" + x));
Spliterator<Integer> trySplit = spliterator.trySplit();
trySplit.forEachRemaining(System.out::println); System.out.println("剩余元素个数,如果无法计算,则返回-1:" + spliterator.getExactSizeIfKnown()); System.out.println("剩下那一半"); spliterator.forEachRemaining(System.out::println); System.out.println("剩余元素个数,此处无限流情况下返回9223372036854775807,一般使用getExactSizeIfKnown:" + spliterator.estimateSize());
System.out.println("比较器:" + Stream.iterate(0, i -> ++i).sorted().spliterator().getComparator());
System.out.println("是否包含Spliterator.SIZED:" + spliterator.hasCharacteristics(Spliterator.SIZED));
System.out.println("spliterator的特征值" + spliterator.characteristics());
StreamSupport.stream(spliterator, false);
StreamSupport.stream(spliterator, true);
|
然后是其中包含的一些特征
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static final int ORDERED = 0x00000010;
public static final int DISTINCT = 0x00000001;
public static final int SORTED = 0x00000004;
public static final int SIZED = 0x00000040;
public static final int NONNULL = 0x00000100;
public static final int IMMUTABLE = 0x00000400;
public static final int CONCURRENT = 0x00001000;
public static final int SUBSIZED = 0x00004000;
|