uniqueElements().iterator() returns broken iterator

Paul Sandoz paul.sandoz at oracle.com
Fri Nov 30 09:21:57 PST 2012


Thanks.

Note that the EG is still cogitating on what to be about nulls. Until agreement is reached this area is up for change.

However, i will fix things next week so nulls are not ignored and java.util.NoSuchElementException is not thrown on iteration.

Paul.


On Nov 30, 2012, at 5:28 PM, Georgiy Rakov <georgiy.rakov at oracle.com> wrote:

> Hello again,
> 
> when executing uniqueElements() on stream consisting of null elements broken iterator is returned.
> 
> Consider following code.
> 
>   import java.util.ArrayList;
>   import java.util.Arrays;
>   import java.util.Iterator;
>   import java.util.List;
> 
>   public class UniqueElementsIssue3 {
>        public static void main(String arg[]) {
>            Iterator<Integer> it = Arrays.asStream(new Integer[]{null,
>   null, null}).uniqueElements().iterator();
>            Integer i = null;
>            List<Object> result1 = new ArrayList<>();
>            while (it.hasNext()) {
>                i = it.next();
>                result1.add(i);
>            }
>            System.out.println("resul1=" +
>   Arrays.toString(result1.toArray()));
>        }
>   }
> 
> This code throws the exception with following stacktrace.
> 
>   Exception in thread "main" java.util.NoSuchElementException
>        at java.util.streams.ops.UniqOp$2.next(UniqOp.java:118)
>        at UniqueElementsIssue3.main(UniqueElementsIssue3.java:19)
> 
> The reason is obvious: it.next() fails while it.hasNext() returns true.
> This occurs for stream in parallel mode as well.
> 
> Could you please tell when we could expect this bug to be fixed (and others on uniqueElements - see two my previous letters).
> 
> Source code is attached for your convenience.
> 
> Thanks,
> Georgiy,
> <UniqueElementsIssue3.java>



More information about the lambda-dev mailing list