Compare two integer arrays using Java Stream

  • A+

I have two integer arrays e.g. -

int[] a = {2, 7, 9}

int[] b = {4, 2, 8}

I want to compare it element by element i.e. 2 to 4 then 7 to 2 and finally 9 to 8. Each comparison result will be stored in a list.

This is pretty easy to do in the traditional Java ways. But I want to use Stream here. Any pointers?


You may do it like so,

List<Boolean> equalityResult = IntStream.range(0, a.length).mapToObj(i -> a[i] == b[i])                 .collect(Collectors.toList()); 

Precondition: both the arrays are of same size.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: