在guava库中,自带了过滤器(filter)的功能,可以用来对collection 进行过滤,先看例子:

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import java.util.List; public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
Iterable<String> result = Iterables.filter(names, Predicates.containsPattern("a"));
// [Jane, Adam]
System.out.println(result);
}
}

在这个例子中,给出一个list,过滤出含有字母a的元素 
此外,可以使用Collections2.filter() 去进行过滤

import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists; import java.util.Collection;
import java.util.List; public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
Collection<String> result = Collections2.filter(names, Predicates.containsPattern("a"));
// [Jane, Adam]
System.out.println(result);
}
}

再来看下predicates判断语言, 
com.google.common.base. Predicate : 根据输入值得到 true 或者 false 
拿Collections2中有2个函数式编程的接口:filter , transform ,例如 :在Collection<Integer>中过滤大于某数的内容:

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists; import java.util.Collection;
import java.util.List; public class Test {
public static void main(String[] args) {
List<Integer> collections = Lists.newArrayList(1, 2, 3, 4);
Collection<Integer> filter = Collections2.filter(
collections, new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input >= 3;
}
});
// [3, 4]
System.out.println(filter);
}
}

把Lis<Integer>中的Integer类型转换为String , 并添加test作为后缀字符

import com.google.common.base.Function;
import com.google.common.collect.Lists; import java.util.List; public class Test {
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1, 2, 3, 4);
List<String> transform = Lists.transform(list, new Function<Integer, String>() {
@Override
public String apply(Integer input) {
return input + "_test";
}
});
// [1_test, 2_test, 3_test, 4_test]
System.out.println(transform);
}
}

需要说明的是每次调用返回都是新的对象,同时操作过程不是线程安全的。

将多个prdicate进行组合

import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists; import java.util.Collection;
import java.util.List; public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
Collection<String> result = Collections2.filter(names,
Predicates.or(Predicates.containsPattern("J"),
Predicates.not(Predicates.containsPattern("a"))));
// [John, Jane, Tom]
System.out.println(result);
}
}

上面的例子中找出包含J字母或不包含a的元素; 
再看下如何将集合中的空元素删除:

import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists; import java.util.Collection;
import java.util.List; public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", null, "Jane", null, "Adam", "Tom");
Collection<String> result = Collections2.filter(names, Predicates.notNull());
// [John, Jane, Adam, Tom]
System.out.println(result);
}
}

检查一个collection中的所有元素是否符合某个条件:

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import java.util.List; public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); boolean result = Iterables.all(names, Predicates.containsPattern("n|m"));
// true
System.out.println(result);
result = Iterables.all(names, Predicates.containsPattern("a"));
// false
System.out.println(result);
}
}

最新文章

  1. tst、cmp、bne、beq指令
  2. CSS中的rem的换算
  3. 『TCP/IP详解——卷一:协议』读书笔记——17
  4. Windows Azure HandBook (1) IaaS相关技术
  5. (视频)《快速创建网站》 4.2 完结篇 – 应用运营vs.发射卫星,遥测(Telemetry) 技术
  6. 一模 (6) day1
  7. OpenCV之响应鼠标(二):函数cvSetMouseCallback()和其副程式onMouse()的使用(OpenCV2.4.5)
  8. 二十四种设计模式:适配器模式(Adapter Pattern)
  9. hdu 5224 Tom and paper
  10. IIS 之 失败请求跟踪规则
  11. Ext.form.FormPanel定义的参数说明
  12. Ubuntu 14.04安装Sogou输入法
  13. android App Widgets
  14. 图解Javascript引用类型之数组
  15. MysqlRouter 实现mysql5.6读写分离
  16. Linux input子系统 io控制字段【转】
  17. springboot~openfeign从此和httpClient说再见
  18. Java(20)file i/o
  19. 前后端分离djangorestframework——认证组件
  20. springmvc多个视图解析器

热门文章

  1. React Native Android启动白屏的一种解决方案上
  2. Java中static块执行时机
  3. C# 读取CSV和EXCEL文件示例
  4. phpexcel错误 You tried to set a sheet active by the out of bounds index: 1解决办法
  5. Slickflow.NET 开源工作流引擎高级开发(二) -- 流程快速测试增值服务工具介绍
  6. Unity3D对安卓盒子的支持
  7. securecrt中文乱码以及ubuntu设置locale
  8. 如何运行Hadoop自带的例子
  9. C++11 bind
  10. 在ASP.NET Web API中使用OData的Action和Function