spliterator

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

给你一囗甜甜゛ 提交于 2020-01-11 06:21:05
问题 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()

Why overload the varargs method of() in Java Stream interface?

可紊 提交于 2019-12-29 00:08:16
问题 The Stream interface has two overloads for the method of() . One of these is a variable-arity method while the other takes a single argument. Is the single-argument method a performance optimization versus passing one argument to the variable-arity method? If so, how does it improve performance? The same questions could be asked of the empty() method, which would seem to be syntax sugar around the variable-arity of() . I see that the implementation differs between these methods, with the

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

孤者浪人 提交于 2019-12-22 05:22:08
问题 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

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

元气小坏坏 提交于 2019-12-18 12:06:06
问题 This question already has answers here : Why does Iterable<T> not provide stream() and parallelStream() methods? (3 answers) Closed 5 years ago . 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

Understanding deeply spliterator characteristics

落爺英雄遲暮 提交于 2019-12-17 08:32:26
问题 In order to try to deeply understand java streams and spliterators, I have some subtle questions about spliterator characteristics : Q1: Stream.empty() vs Stream.of() (Stream.of() without args) Stream.empty() : SUBSIZED, SIZED Stream.of() : SUBSIZED, IMMUTABLE , SIZED, ORDERED Why Stream.empty() doesn't have the same characteristics of Stream.of() ? Note that it has impacts when using in conjunction with Stream.concat() (specially not having ORDERED ). I would say that Stream.empty() should

Understanding deeply spliterator characteristics

依然范特西╮ 提交于 2019-12-17 08:31:50
问题 In order to try to deeply understand java streams and spliterators, I have some subtle questions about spliterator characteristics : Q1: Stream.empty() vs Stream.of() (Stream.of() without args) Stream.empty() : SUBSIZED, SIZED Stream.of() : SUBSIZED, IMMUTABLE , SIZED, ORDERED Why Stream.empty() doesn't have the same characteristics of Stream.of() ? Note that it has impacts when using in conjunction with Stream.concat() (specially not having ORDERED ). I would say that Stream.empty() should

Java 8 Iterator to stream to iterator causes redundant call to hasNext()

人走茶凉 提交于 2019-12-13 14:29:06
问题 I notice a bit of a strange behavior in the following scenario: Iterator -> Stream -> map() -> iterator() -> iterate The hasNext() of the original iterator is called an additional time after having already returned false. Is this normal? package com.test.iterators; import java.util.Iterator; import java.util.Spliterators; import java.util.stream.Stream; import java.util.stream.StreamSupport; public class TestIterator { private static int counter = 2; public static void main(String[] args) {

Why Hashmap.values().parallelStream() does not run in parallel while wrap them in ArrayList could work?

ε祈祈猫儿з 提交于 2019-12-11 06:26:07
问题 The hashmap has two key and value pairs, they are not processed in parallel by different threads. import java.util.stream.Stream; import java.util.Map; import java.util.HashMap; class Ideone { public static void main (String[] args) throws java.lang.Exception { Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.values().parallelStream() .peek(x -> System.out.println("processing "+x+" in "+Thread.currentThread())) .forEach(System.out::println); } } Output:

Unsplittable Spliterators

天涯浪子 提交于 2019-12-09 09:10:47
问题 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,

Making an efficient Java 8 sorted Spliterator from an array

扶醉桌前 提交于 2019-12-08 14:42:21
问题 In Java 8, a variety of convenient utilities are provided to build efficient Spliterators from arrays. However, no factory methods are provided there to build a Spliterator with a comparator. Clearly Spliterators are allowed to have attached comparators; they have a getComparator() method and a SORTED property. How are library authors supposed to build SORTED Spliterators? 回答1: It seems that it is not foreseen to have such a Spliterator with an order other than natural. But implementing it is