在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);
}
}

Guava中针对集合的 filter和过滤功能的更多相关文章

  1. js 根据条件删除数组中某个对象&js filter (find)过滤数组对象的使用

    删除 ----  item不设置 arr.splice(1,1)   //['a','c','d']         删除起始下标为1,长度为1的一个值,len设置的1,如果为0,则数组不变 arr. ...

  2. Guava中Predicate的常见用法

    Guava中Predicate的常见用法 1.  Predicate基本用法 guava提供了许多利用Functions和Predicates来操作Collections的工具,一般在 Iterabl ...

  3. [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具

    转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...

  4. 使用filter方法过滤集合元素

    文章转自https://my.oschina.net/nenusoul/blog/658238 Problem 你想要筛选出集合中的一些元素形成一个新的集合,这些元素都是满足你的筛选条件的. Solu ...

  5. addBack() 添加堆栈中元素集合到当前集合,一个选择性的过滤选择器。

    addBack() 概述 添加堆栈中元素集合到当前集合,一个选择性的过滤选择器. 如上所述在讨论中的.end(), jQuery对象维护一个堆栈内部来跟踪匹配的元素集合的变化.当一个DOM遍历方法被调 ...

  6. Java经典类库-Guava中的函数式编程讲解

    如果我要新建一个java的项目,那么有两个类库是必备的,一个是junit,另一个是Guava.选择junit,因为我喜欢TDD,喜欢自动化测试.而是用Guava,是因为我喜欢简洁的API.Guava提 ...

  7. Scala_针对集合的操作

    针对集合的操作 遍历操作 列表的遍历 scala> val list = List(1,2,3,4,5,6) list: List[Int] = List(1, 2, 3, 4, 5, 6) s ...

  8. Python中针对函数处理的特殊方法

    Python中针对函数处理的特殊方法 很多语言都提供了对参数或变量进行处理的机制,作为灵活的Python,提供了一些针对函数处理的特殊方法 filter(function, sequence):对se ...

  9. ☕【Java技术指南】「Guava Collections」实战使用相关Guava不一般的集合框架

    Google Guava Collections 使用介绍 简介 Google Guava Collections 是一个对 Java Collections Framework 增强和扩展的一个开源 ...

随机推荐

  1. grep 详解

    grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.(global search regular expression(RE) and print out the l ...

  2. IO读 BufferedReader+FileReader

    private static final String FILENAME = "c:\\temp\\in.txt"; public static void main(String[ ...

  3. VA插件突然不能使用,彈出“the security key for....”

    昨天打開VS莫名其妙地彈出下面的錯誤框: "the security key for this program currently stored on your system does no ...

  4. Outdated Kotlin Runtime

    你的 kotlin 运行时版本 在 1.1.2库中 是 1.1.2 然而插件版本是 1.1.4 . 运行时库 应该被更新,避免兼容问题. Outdated Kotlin Runtime Your ve ...

  5. BZOJ1170 : [Balkan2007]Cipher

    首先对于每个位置,求出它开始长度为y的横行的hash值,然后对于hash值再求一次竖列的hash值,排序后求出众数即可. 时间复杂度$O(n^2\log n)$. #include<cstdio ...

  6. Gunicorn部署部分的翻译

    部署Gunicorn 文档建议Gunicorn最好是用在代理服务器后面.(等于前面最好加一个反向代理) Nginx Configuration 文档建议用Nginx,当然用其他也可以,但是要确保当你用 ...

  7. 微信小程序自定义组件封装及父子间组件传值

    首先在我们可以直接写到需要的 page 中,然后再进行抽取组件,自定义组件建议 wxzx-xxx 命名 官网地址:https://developers.weixin.qq.com/miniprogra ...

  8. 使用 IntraWeb (9) - JavaScript

    IW 依赖 js 构建(我数了数, 在当前版本它的资源文件默认携带了 26 个 js); 但 IW 尽可能地让用户少用或不用 js, 但如果你对 js 也不陌生, IW 提供了多种途径与方便. 我给它 ...

  9. JTAG Communications model

    https://en.wikipedia.org/wiki/Joint_Test_Action_Group In JTAG, devices expose one or more test acces ...

  10. USBDM Debugger interface for Freescale RS08,HCS08,HCS12,Coldfire and ARM-Kinetis Devices.

    Introduction USBDM is a debugger hardware interface for a range of Freescale microcontrollers. It is ...