Guava中Predicate的常见用法
Guava中Predicate的常见用法
1. Predicate基本用法
guava提供了许多利用Functions和Predicates来操作Collections的工具,一般在 Iterables, Lists, Sets, Maps, Multimaps中用到。
Predicate最基本的用法就是对Collection进行过滤,guava中很多集合的filter方法都是用Predicate来实现过滤的。
注意:
Lists没有提供filter方法;
过滤后的集合一般通过Lists.newArrayList(Collections2.filter(list, predicate))拿到。
2. Predicate接口
Predicate接口提供了一个泛型方法apply,在使用时根据需求实现
Predicate继承了Object的equals方法,并提供了多个实现,主要是为了提供一个通用的方法,用于Object为Predicate类型时。

package com.google.common.base; import com.google.common.annotations.GwtCompatible; import javax.annotation.Nullable; @GwtCompatible
public interface Predicate<T> { boolean apply(@Nullable T input); @Override
boolean equals(@Nullable Object object);
}

3. Predicates的常用方法
Predicates时guava中与Predicate配套使用的工具类,返回Predicate实例。
下面是一个例子

package link.mengya; /**
- Created by chang on 16/2/19.
*/
public class User {
private String userName;
private int age; public User(String userName, int age) {
this.userName = userName;
this.age = age;
} public String getUserName() {
return userName;
} public int getAge() {
return age;
} public void setUserName(String userName) {
this.userName = userName;
} public void setAge(int age) {
this.age = age;
}
}


package link.mengya.utils; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import link.mengya.User; import java.util.ArrayList;
import java.util.List;
import java.util.Objects; /**
- Created by chang on 16/2/19.
*/
- Predicate 返回为true 的保留, 返回为false的过滤掉
- Predicates.and(predicate1, predicate2) predicate1 与 predicate2 返回都为true的保留
- Predicates.or(predicate1, predicate2) predicate1 与 predicate2 有一个返回true 则保留
*/
public class PredicateTest {
public static void main(String[] args){
List<User> users = new ArrayList<User>();
users.add(new User("chang",24));
users.add(new User("chen",26));
users.add(new User("sun",24));</span><span style="color: #008000;">//</span><span style="color: #008000;">保留age不为26的User</span>
Predicate<User> predicate1 = <span style="color: #0000ff;">new</span> Predicate<User><span style="color: #000000;">() {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;"> apply(User user) {
</span><span style="color: #0000ff;">if</span>(user.getAge() != 26<span style="color: #000000;">){
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}
}; </span><span style="color: #008000;">//</span><span style="color: #008000;">保留userName 是 chang 的user</span>
Predicate<User> predicate2 = <span style="color: #0000ff;">new</span> Predicate<User><span style="color: #000000;">() {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;"> apply(User user) {
</span><span style="color: #0000ff;">return</span> Objects.equals(user.getUserName(),"chang"<span style="color: #000000;">);
}
}; </span><span style="color: #008000;">//</span><span style="color: #008000;">保留age不为 26 以及 userName 是 chang 的User</span>
Predicate<User> predicate1_and_predicate2 =<span style="color: #000000;"> Predicates.and(predicate1, predicate2); </span><span style="color: #008000;">//</span><span style="color: #008000;">保留age不为26 或 userName 是 chang的User</span>
Predicate<User> predicate1_or_predicate2 =<span style="color: #000000;"> Predicates.or(predicate1, predicate2); </span><span style="color: #008000;">//</span><span style="color: #008000;">与predicate1条件相反</span>
Predicate<User> notpredicate1 =<span style="color: #000000;"> Predicates.not(predicate1); </span><span style="color: #008000;">//</span><span style="color: #008000;">List<User> filteredUsers = Lists.newArrayList(Iterators.filter(users.iterator(), predicate1));</span>
List<User> filteredUsers1 =<span style="color: #000000;"> Lists.newArrayList(Iterables.filter(users,predicate1));
List</span><User> filteredUsers2 =<span style="color: #000000;"> Lists.newArrayList(Iterables.filter(users,predicate2));
List</span><User> filteredUsers1and2 =<span style="color: #000000;"> Lists.newArrayList(Iterables.filter(users,predicate1_and_predicate2));
List</span><User> filteredUsers1or2 =<span style="color: #000000;"> Lists.newArrayList(Iterables.filter(users,predicate1_or_predicate2)); List</span><User> filteredUsersNot1 =<span style="color: #000000;"> Lists.newArrayList(Iterables.filter(users,notpredicate1)); System.out.println(</span>"result size for filteredUsers1: " + filteredUsers1.size()); <span style="color: #008000;">//</span><span style="color: #008000;">2-> chang sun</span>
System.out.println("result size for filteredUsers2: " + filteredUsers2.size()); <span style="color: #008000;">//</span><span style="color: #008000;">1-> chang</span>
System.out.println("result size for filteredUsers1and2: " + filteredUsers1and2.size()); <span style="color: #008000;">//</span><span style="color: #008000;">1-> chang</span>
System.out.println("result size for filteredUsers1or2: " + filteredUsers1or2.size()); <span style="color: #008000;">//</span><span style="color: #008000;">2-> chang sun</span>
System.out.println("result size for filteredUsersNot1: " + filteredUsersNot1.size()); //1-> chen
}
}

更多关于guava中Predicates与Functions的用法参见
guava-libraries的wiki: https://code.google.com/p/guava-libraries/wiki/FunctionalExplained
guava github上的wiki:https://github.com/google/guava/wiki/FunctionalExplained#predicates
</div>
<div class="postDesc">posted @ <span id="post-date">2016-02-20 13:39</span> <a href="http://www.cnblogs.com/onlychang92/">chang20159</a> 阅读(<span id="post_view_count">494</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=5203139" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(5203139);return false;">收藏</a></div>
</div>
Guava中Predicate的常见用法的更多相关文章
- JAVA中enum的常见用法
JAVA中enum的常见用法包括:定义并添加方法.switch.遍历.EnumSet.EnumMap 1.定义enum并添加或覆盖方法 public Interface Behaviour{ void ...
- Java中枚举的常见用法
在JDK1.5以后引入了一种新的类型,就是枚举(enum).enum是用来声明枚举类型数据,它可以像数组一样存储许多的元素,但是不同于数组的是,它除了数字不能存储以外, 其他类型的如字母.特殊符号.汉 ...
- (转)轻松掌握shell编程中数组的常见用法及示例
缘起:在老男孩进行linux培训shell编程教学中,发现不少水平不错的网友及同学对数组仍然很迷糊,下面就给大家分享下数组的用法小例子,希望能给大家一点帮助.其实SHELL的数组很简单,好用.我们学习 ...
- Objective-C中#define的常见用法
参考博客 http://blog.csdn.net/kindazrael/article/details/8108868 在C语言中,预处理代码是非常强大的工具,能让代码变得可读性和可维护性更强.预处 ...
- 转:VC中MessageBox的常见用法
一.关于MessageBox 消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.MessageBox("这是一个最简单的 ...
- VC中MessageBox的常见用法
一.关于MessageBox 消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.MessageBox("这是一个最简单的 ...
- K:java中枚举的常见用法
用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. ...
- VC++中MessageBox的常见用法详解
消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.MessageBox("这是一个最简单的消息框!"); ...
- Objective-C中Block的常见用法
typedef int(^AddValue)(int,int); int main(int argc, const char * argv[]) { @autoreleasepool { //1:NS ...
随机推荐
- jQuery绑定事件的四种方式
jQuery提供了多种绑定事件的方式,每种方式各有其特点,明白了它们之间的异同点,有助于我们在写代码的时候进行正确的选择,从而写出优雅而容易维护的代码.下面我们来看下jQuery中绑定事件的方式都 ...
- EXCEL快速自动填充方法集锦
EXCEL快速自动填充方法集锦 原文地址,转载请注明:http://www.cnblogs.com/croso/p/5396841.html 方法一: 名称框输入a1:a1000回车,1, ctrl+ ...
- 墙内下载DropBox离线安装包的方法
让你的浏览器FQ,然后进入官网,点击下载,你会看到这个网页: 现在,只需要在原下载的链接后加上“&full=1”即可.就像这样: https://www.dropbox.com/downloa ...
- Android存储访问及目录
Android存储访问及目录 Android的外部存储 Android支持外部存储(case-insensitive filesystem with immutable POSIX permissio ...
- sleep() 和 wait() 的区别
好多面经上都出现了,有必要好好熟悉一下 区别: 1.wait() 可以指定时间,也可以不指定(等五分钟你进来,或者是不叫你一直等着):sleep()必须指定时间(不能一睡不起) 2.wait()是Ob ...
- MVC 生成图片,下载文件(图片不存在本地,在网上下载)
/// <summary> /// 生成图片 /// </summary> /// <param name="collection"></ ...
- Kotlin偏好设置
Kotlin的强悍震精了我,android中每个应用都会用到SharedPreference在Kotlin中使用竟是如此简单! package com.android.extkt import and ...
- Android 学习资料收集
收集整理这份资料灵感来自于 trip_to_iOS, 征得同意引用了该资料的开头描述 收集整理这份资料主要帮助初学者学习 Android 开发, 希望能快速帮助到他们快速入门, 找到适合自己学习资料, ...
- html中相似的标签、属性的区别
[1]<i></i> 和 <em></em>标签 相同:都是表示斜体. 区别: (1)<em>表示被强调呈现的内容,<i>是物理 ...
- (转)教你记住ASP.NET WebForm页面的生命周期
对于ASP.NET Webform的开发者,理解ASP.NET Webform的页面生命周期是非常重要的.主要是为了搞明白在哪里放置特定的方法和在何时设置各种页面属性.但是记忆和理解页面生命周期里提供 ...