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.
*//**
System.out.println("result size for filteredUsersNot1: " + filteredUsersNot1.size()); //1-> chen
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>
}
}
更多关于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
服务器间文件实时双向同步(rsync+inotify)
全链路追踪 & 性能监控工具 SkyWalking 实战
TCP Dup ACK linux kernel 3.2
Ns3.35 errata Compilation on Python 3.10 systems
LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 98. Validate Binary Search Tree
LeetCode 701. Insert into a Binary Search Tree
LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
LeetCode 783. Minimum Distance Between BST Nodes
- 最新文章
-
CF1688B Patchouli's Magical Talisman
leetcode2271 毯子覆盖的最多白色砖块数
leetcode2276 统计区间中的整数数目
leetcode887 鸡蛋掉落
2019 google kickstart round D food stalls
leetcode2272 最大波动的子字符串
leetcode732 我的日程安排表 III
leetcode829 连续整数求和
用户态与内核态之间切换详解
锁引命名
- 热门文章
-
juc
mapstruct原理解析
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint
Swagger
# openlayer 绘制扇形
# cesium 绘制栅栏
# Openlayers 接入 Geojson 文件实现行政区域边界绘制
扩展 openpyxl 对 Excel 中自定义单元格格式的处理
websec + fucking hacker / auto.c3pool.org:19999 / Monero
OS + Centos Steam 8 / sshd_config / ssh_config / sshd / sshd.service / ssh / 22222