Compare two integer arrays using java stream
•
Java
I have two arrays of integers, for example –
int [] a = {2,7,9}
int [] b = {4,2,8}
I want to compare it element by element, that is, 2 to 4, then 7 to 2, and finally 9 to 8 Each comparison result is stored in a list
It's easy to do this using traditional Java methods But I want to use stream here Do you have any pointers?
Solution
You can do this,
List<Boolean> equalityResult = IntStream.range(0,a.length).mapToObj(i -> a[i] == b[i]) .collect(Collectors.toList());
Prerequisite: the two arrays are the same size
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码