2

I wrote this piece of code:

private double[] myArray = {1.0, 2.4, 9.3};

public void testMet(double value){
    if(Arrays.asList(myArray).stream().noneMatch(a -> a==value)){
       ...
    }
}

I am getting the error Operator '==' cannot be applied to 'double[]', 'double. What is the problem here and how can I fix it?

ofr13
  • 23
  • 3

1 Answers1

7

Arrays.asList(myArray) returns List<double[]> with myArray as its singular element; see here for details. Use Arrays.stream() instead:

Arrays.stream(myArray).noneMatch(a -> a==value)
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 1
    The headliner here is really in the "here for details". It is not so obvious from the API docs for `Arrays.asList()` that it will have the result it does for arguments that are arrays of primitives. The pre-varargs docs were much clearer in this regard. – John Bollinger Feb 02 '18 at 22:45
  • @JohnBollinger before the introduction of varargs, this kind of error could not happen, as you got a compiler error right when attempting to pass a primitive array to `Arrays.asList`. In retrospect, the retrofitting of `asList` to a varargs method has been considered a design mistake. That’s why `Arrays.stream` is not a varargs method, you can clearly tell apart intent by the choice between `Array.stream(array)` or `Stream.of(some, named, items)`. – Holger Feb 05 '18 at 13:36