spliterator

Is there any danger in making the action .accept() more than one element in an implementation of Spliterator's .tryAdance()?

被刻印的时光 ゝ 提交于 2019-12-07 03:09:29
问题 The javadoc of Spliterator mentions that: A Spliterator may traverse elements individually (tryAdvance()) or sequentially in bulk (forEachRemaining()). Then we go to the javadoc of tryAdvance() which says that: If a remaining element exists, performs the given action on it, returning true; else returns false. Maybe I am misreading somewhere, but to me it seems that provided there is one element, or more , remaining, the Consumer as an argument should only every .accept() one argument before

Strange behavior of Stream.spliterator for parallel streams

最后都变了- 提交于 2019-12-06 20:15:58
问题 I'm using the stream spliterator directly for the low-level operations in the library I'm writing. Recently I discovered very weird behavior when I take the stream spliterator and interleave tryAdvance/trySplit calls. Here's a simple code which demonstrates the problem: import java.util.Arrays; import java.util.Spliterator; public class SpliteratorBug { public static void main(String[] args) { Integer[][] input = { { 1 }, { 2, 3 }, { 4, 5, 6 }, { 7, 8 }, { 9 } }; Spliterator<Integer>

Java 8 Spliterator (or similar) that returns a value iff there's only a single value

只愿长相守 提交于 2019-12-06 02:27:56
问题 I'm a big fan of the singleOrEmpty stream operator. It's not in the std lib, but I find it very useful. If a stream has only a single value, it returns that value in an Optional . If it has no values or more than one value, it returns Optional.empty() . Optional<Int> value = someList.stream().{singleOrEmpty} [] -> Optional.empty() [1] -> Optional.of(1) [1, 1] -> Optional.empty() etc. I asked a question about it earlier and @ThomasJungblut came up with this great implementation: public static

Weird loops used in Spliterator of Java 8 [duplicate]

天大地大妈咪最大 提交于 2019-12-05 18:42:48
This question already has an answer here: Do “nothing” while “condition” 5 answers Does anyone know why the java.util.Spliterator implementation uses do-while rather than while loops, when the body of the loop is empty? For instance, the implementation of forEachRemaining is: default void forEachRemaining(Consumer<? super T> action) { do { } while (tryAdvance(action)); } Why would they use do { } while (tryAdvance(action)); instead of while(tryAdvance(action)); ? Are there any advantages I am not aware of? The logic check executes after the body of the do{}. This can be utilized many ways but

How to restrict a Stream to run sequentially, and prevent it from running in parallel?

假如想象 提交于 2019-12-05 06:45:12
I have a method that returns a stream that is generated from a custom spliterator; the spliterator is not tread safe. Since the spliterator is not tread safe, and it maintains state, I want to prevent it from running in parallel. Is there a way to prevent the returned stream from running in parallel? I have not been able to find any documentation or examples that do this. I did find a sequential() method on the BaseStream class, but that does not appear to prevent a user from then calling parallel() to get a parallel stream. Parallel stream calls trySplit() method of your spliterator to split

Difference between Iterator and Spliterator in Java8

梦想与她 提交于 2019-12-03 12:45:21
问题 I came to know while studying that Parallelism is a main advantage of Spliterator . This may be a basic question but can anyone explain me the main differences between Iterator and Spliterator and give some examples? 回答1: The names are pretty much self-explanatory, to me. Spliterator == Splittable Iterator : it can split some source, and it can iterate it too. It roughly has the same functionality as an Iterator , but with the extra thing that it can potentially split into multiple pieces:

Unsplittable Spliterators

▼魔方 西西 提交于 2019-12-03 12:27:35
I'm trying to understand how Spliterator works, and how spliterators are designed. I recognize that trySplit() is likely one of the more important methods of Spliterator , but when I see some third-party Spliterator implementations, sometimes I see that their spliterators return null for trySplit() unconditionally. The questions: Is there a difference between an ordinary iterator and a Spliterator that returns null unconditionally? It seems like such a spliterator defeats the point of, well, splitting. Of course, there are legitimate use cases of spliterators that conditionally return null on

Java 8 Stream of Super Classes, Parent Files, Component Parents, linked list, etc

帅比萌擦擦* 提交于 2019-12-01 20:33:48
问题 I would like to convert the following for statement to a Java 8 stream (i.e. Stream<Class<?>> ). The ideal solution would be simple enough that I can easily adapt it for a variety of situations of traversing a linked list (e.g. File.getParentFile() , Component.getParent() ). Class<?> clazz; Object value; value = ...; for (clazz = value.getClass(); clazz != null; clazz = clazz.getSuperclass()) ... I realize that a few lines of code to create a stream isn't going to be simpler than a single for

Understanding sequential vs parallel stream spliterators in Java 8 and Java 9

自古美人都是妖i 提交于 2019-12-01 09:01:14
A question about spliterators that at first glance is not straightforward. In streams, .parallel() changes the behaviour that the stream is processed. However I was expecting the spliterators created from sequential and parallel streams to be the same. For example, in sequential streams typically, the .trySplit() is never invoked , while in parallel streams it is, in order to hand over the split spliterator to another thread. Differences between stream.spliterator() vs stream.parallel().spliterator() : They may have different characteristics: Stream.of(1L, 2L, 3L).limit(2); // ORDERED Stream

How to perform Stream functions on an Iterable? [duplicate]

岁酱吖の 提交于 2019-11-30 05:43:13
This question already has an answer here: Why does Iterable<T> not provide stream() and parallelStream() methods? 3 answers In Java 8, the Stream class does not have any method to wrap a an Iterable . Instead, I am obtaining the Spliterator from the Iterable and then obtaining a Stream from StreamSupport like this: boolean parallel = true; StreamSupport.stream(spliterator(), parallel) .filter(Row::isEmpty) .collect(Collectors.toList()) .forEach(this::deleteRow); Is there some other way of generating Stream operations on an Iterable that I am missing? My similar question got marked as duplicate