Groovy difference between the "any" and "find" methods

There are two methods in groovy: the method anyand findwhich can be used in Maps.

Both of these methods will "seek" we are interested in the content (ie, the method anyand findreturns, whether in the Map element or not, that is, they need to look for).

But in this search, how are they different?

+3
source share
3 answers

You looked at the documentation for findandany

Hint: the difference is in the inverse type

+7
source
 (!list.find{predicate}) <> (!list.any{predicate})

However:

( list.find{predicate}) >< (list.any{predicate})

If there are no objects in the Groovy API and you want to add this function to the List metClass, any implementation will be:

java.util.List.metaClass.any={Closure c-> 
     return delegate.find(c) != null

}

Find more general than any

+1

. find , , any bool. groovy.

unset (null?) false

def x
assert !x

So, if you just check the false value, the return values โ€‹โ€‹of both methods will serve the same purpose, since essentially all objects have an implicit existential logical value.

+1
source

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


All Articles