I have an Array and want to perform some matching on it's element.
I came to know that it could 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 can understand both are doing the same work. However, I could not found which one to prefer?
Could someone please make it clear what is the difference between both of them.
解决方案
They do the same job internally, but their return value is different. Stream#anyMatch() returns a boolean while Stream#findAny() returns an object which matches the predicate.