guava中Collection的transform遇到需要跳过的处理

在使用GUAVA的集合transform时,经常需要碰到去null,异常的元素,怎么处理?

使用Optional!

 

 1 Iterable<Rds> iterable = Optional.presentInstances(Lists.transform(serviceInstances, new Function<ServiceInstance, Optional<Rds>>() {
 2     @Nullable
 3     @Override
 4     public Optional<Rds> apply(@Nullable ServiceInstance input) {
 5         if (remove) {
 6             return Optional.absent();
 7         }
 8         return Optional.of(buildRds(input));
 9     }
10 }));

 

 

 

 

 

posted @ 2014-03-03 11:33  小玄子的后花园  阅读(393)  评论(0)    收藏  举报