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)); //保留age不為26的User Predicate<User> predicate1 = new Predicate<User>() { public boolean apply(User user) { if(user.getAge() != 26){ return true; } return false; } }; //保留userName 是 chang 的user Predicate<User> predicate2 = new Predicate<User>() { public boolean apply(User user) { return Objects.equals(user.getUserName(),"chang"); } }; //保留age不為 26 以及 userName 是 chang 的User Predicate<User> predicate1_and_predicate2 = Predicates.and(predicate1, predicate2); //保留age不為26 或 userName 是 chang的User Predicate<User> predicate1_or_predicate2 = Predicates.or(predicate1, predicate2); //與predicate1條件相反 Predicate<User> notpredicate1 = Predicates.not(predicate1); //List<User> filteredUsers = Lists.newArrayList(Iterators.filter(users.iterator(), predicate1)); List<User> filteredUsers1 = Lists.newArrayList(Iterables.filter(users,predicate1)); List<User> filteredUsers2 = Lists.newArrayList(Iterables.filter(users,predicate2)); List<User> filteredUsers1and2 = Lists.newArrayList(Iterables.filter(users,predicate1_and_predicate2)); List<User> filteredUsers1or2 = Lists.newArrayList(Iterables.filter(users,predicate1_or_predicate2)); List<User> filteredUsersNot1 = Lists.newArrayList(Iterables.filter(users,notpredicate1)); System.out.println("result size for filteredUsers1: " + filteredUsers1.size()); //2-> chang sun System.out.println("result size for filteredUsers2: " + filteredUsers2.size()); //1-> chang System.out.println("result size for filteredUsers1and2: " + filteredUsers1and2.size()); //1-> chang System.out.println("result size for filteredUsers1or2: " + filteredUsers1or2.size()); //2-> chang sun 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