uniqueElements().iterator() filters nulls out

Paul Sandoz paul.sandoz at oracle.com
Fri Nov 30 08:05:50 PST 2012


Thanks for sending code as well, very helpful!

I can reproduce on the tip.

When pulling null is currently used as a sentinel value, easy to fix.

However, you are gonna run into NPEs when going parallel and order is not preserved because ConcurrentHashMap is used.

We need to decide on a consistent behaviour for nulls e.g. throw NPE under all case.

Paul.

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

> Hello,
> 
> if s is a sequential stream than s.uniqueElements().iterator() returns iterator which filters nulls out.
> 
> Consider following code.
> 
>   import java.util.ArrayList;
>   import java.util.Arrays;
>   import java.util.Iterator;
>   import java.util.List;
> 
>   public class UniqueElementsIssue2 {
>        public static void main(String arg[]) {
>            Iterator<Integer> it = Arrays.asStream(1, 2, 3, null, 4,
>   5).uniqueElements().iterator();
>            Integer i = null;
>            List<Object> result1 = new ArrayList<>();
>            while (it.hasNext()) {
>                i = it.next();
>                result1.add(i);
>            }
>            List<Object> result2 = new ArrayList<>();
>            Arrays.asStream(1, 2, 3, null, 4,
>   5).uniqueElements().into(result2);
>            System.out.println("resul1=" +
>   Arrays.toString(result1.toArray()));
>            System.out.println("resul2=" +
>   Arrays.toString(result2.toArray()));
>        }
>   }
> 
> It produces following output:
> 
>   resul1=[1, 2, 3, 4, 5]
>   resul2=[1, 2, 3, null, 4, 5]
> 
> result1 seems to be faulty while result2 is ok (into() works fine).
> 
> As for me it's a bug but anyway could you please confirm that it is.
> 
> Code above is attached for your convenience.
> 
> Thanks,
> Georgiy.
> <UniqueElementsIssue2.java>



More information about the lambda-dev mailing list