我喜欢谷歌番石榴并且使用它很多,但有一种方法我总能找到我写的..
 public static <T> T tryFind(Iterable<T> iterable, Predicate<T> predicate){
     for(T t : iterable){
         if(predicate.apply(t)){
              return t;
         }
     }
     return null;
  }
对我来说,这似乎是一个非常有用的补充Iterables(也是Iterators为了那个问题),所以我想知道它为什么缺失.此外,虽然我可以看到有一个方法抛出的点NoSuchElementException,也许是为了区分找到一个null而没有找到该元素,但只有当你使用的谓词是
public boolean apply(T t){
     return t==null;
}
这似乎不是一个常见的情况.
那么为什么番石榴设计师选择了这种行为,而不是只是在找不到它时才返回null?
这是[Iterables.find()] [1]的javadoc
[1]:http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html#find (java.lang.Iterable,com.google.common.base.谓语)
| 归档时间: | 
 | 
| 查看次数: | 7982 次 | 
| 最近记录: |