我使用谷歌集合,并试图找到第一个满足谓词的元素,如果没有,返回我'null'。Iterables.find和Iterators.find - 而不是抛出异常,得到空
不幸的是,当找不到元素时,Iterables.find和Iterators.find会抛出NoSuchElementException。
现在,我不得不做
Object found = null;
if (Iterators.any(newIterator(...) , my_predicate)
{
found = Iterators.find(newIterator(...), my_predicate)
}
我可以用“try/catch语句”,做同样的事情,但对我的使用情况,我会遇到很多情况下,没有元素环绕被发现。
有没有更简单的方法呢?