Substream applied to unordered streams

Paul Sandoz paul.sandoz at oracle.com
Sun Mar 17 12:30:32 PDT 2013


On Mar 16, 2013, at 9:43 PM, Georgiy Rakov <georgiy.rakov at oracle.com> wrote:
> 
> In general I asked whether you see useful to show these points in spec, 
> e. g. if spec contains something like:
> 
>    You should keep in mind that substream() could return stream which
>    content is nondeterministic provided original stream encounter order
>    is not defined, e.g. for stream returned from HashSet instance.
> 

It cuts deeper than that.

If a source does not have encounter order then by definition traversal of that source, using say an Iterator or a Spliterator, may be non-determinisitic.

This can even apply to the splitting of a Spliterator. If such a Spliterator does not have an encounter order, then when split it is free to partition in anyway appropriate to its internal data structures, for example try this:

        Map<Integer, Integer> m = new ConcurrentHashMap<>();
        for (int i = 0; i < 10000; i++) m.put(i, i);

        List<Integer> lRoot = new ArrayList<>();
        m.keySet().spliterator().forEach(lRoot::add);

        List<Integer> lLeftRight = new ArrayList<>();
        Spliterator<Integer> right = m.keySet().spliterator();
        Spliterator<Integer> left = right.trySplit();
        left.forEach(lLeftRight::add);
        right.forEach(lLeftRight::add);

        System.out.println(lLeftRight.size() == lRoot.size());
        System.out.println(lLeftRight.equals(lRoot));
        System.out.println(new HashSet(lRoot).equals(new HashSet(lLeftRight)));

It should print out:

true
false
true

Paul.


More information about the lambda-dev mailing list