Difference between anyMatch and findAny in java 8

I have Arrayand you want to do some mapping with it.

I found out that this can be done in two ways in java 8:

String[] alphabet = new String[]{"A", "B", "C"};

anyMatch:

Arrays.stream(alphabet).anyMatch("A"::equalsIgnoreCase)

findAny:

Arrays.stream(alphabet).filter("a"::equalsIgnoreCase).findAny().orElse("No match found"))

As I understand it, both are doing the same job. However, I could not find which one was preferable?

Can anyone clarify the difference between the two?

+4
source share
1 answer

They do the same job internally, but their return value is different. Stream#anyMatch()returns a boolean, and Stream#findAny()returns an object that matches the predicate.

+9
source

Source: https://habr.com/ru/post/1677908/


All Articles