Java contains vs anyMatch behaviour
The problem with the stream-based version is that if the collection (and thus its stream) contains null
elements, then the predicate will throw a NullPointerException
when it tries to call equals
on this null
object.
This could be avoided with
boolean exists = names.stream().anyMatch(x -> Objects.equals(x, n));
But there is no practical advantage to be expected for the stream-based solution in this case. Parallelism might bring an advantage for really large lists, but one should not casually throw in some parallel()
here and there assuming that it may make things faster. First, you should clearly identify the actual bottlenecks.
And in terms of readability, I'd prefer the first, classical solution here. If you want to check whether the list of names.contains(aParticularValue)
, you should do this - it just reads like prose and makes the intent clear.
EDIT
Another advantage of the contains
approach was mentioned in the comments and in the other answer, and that may be worth mentioning here: If the type of the names
collection is later changed, for example, to be a HashSet
, then you'll get the faster contains
-check (with O(1) instead of O(n)) for free - without changing any other part of the code. The stream-based solution would then still have to iterate over all elements, and this could have a significantly lower performance.
They should provide the same result if hashCode()
and equals()
are written in reasonable way.
But the performance may be completely different. For Lists it wouldn't matter that much but for HashSet contains()
will use hashCode()
to locate the element and it will be done (most probably) in constant time. While with the second solution it will loop over all items and call a function so will be done in linear time.
If n is null, actually doesn't matter as usually equals()
methods are aware of null
arguments.