Java經典類庫-Guava中的函數式編程講解
如果我要新建一個java的項目,那么有兩個類庫是必備的,一個是junit,另一個是 Guava 。選擇junit,因為我喜歡TDD,喜歡自動化測試。而是用 Guava ,是因為我喜歡簡潔的API。 Guava 提供了很多的實用工具函數來彌補java標準庫的不足,另外 Guava 還引入了函數式編程的概念,在一定程度上緩解了java在JDK1.8之前沒有lambda的缺陷,使使用java書寫簡潔易讀的函數式風格的代碼成為可能。
下面就簡單的介紹下 Guava 中的一些體現了函數式編程的API。
Filter
我們先創建一個簡單的Person類。
Person.java
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
如果要產生一個Person類的List,通常的寫法可能是這樣子。
List<Person> people = new ArrayList<Person>();
people.add(new Person("bowen",27));
people.add(new Person("bob", 20));
people.add(new Person("Katy", 18));
people.add(new Person("Logon", 24));
而 Guava 提供了一個newArrayList的方法,其自帶類型推演,并可以方便的生成一個List,并且通過參數傳遞初始化值。
List<Person> people = newArrayList(new Person("bowen", 27),
new Person("bob", 20),
new Person("Katy", 18),
new Person("Logon", 24));
當然,這不算函數式編程的范疇,這是 Guava 給我們提供的一個實用的函數。
如果我們選取其中年齡大于20的人,通常的寫法可能是這樣子。
List<Person> oldPeople = new ArrayList<Person>();
for (Person person : people) {
if (person.getAge() >= 20) {
oldPeople.add(person);
}
}
這就是典型的filter模式。filter即從一個集合中根據一個條件篩選元素。其中person.getAge() >=20就是這個條件。 Guava 為這種模式提供了一個filter的方法。所以我們可以這樣寫。
List<Person> oldPeople = newArrayList(filter(people, new Predicate<Person>() {
public boolean apply(Person person) {
return person.getAge() >= 20;
}
}));
這里的Predicate是 Guava 中的一個接口,我們來看看它的定義。
Predicate.java
@GwtCompatible
public interface Predicate<T> {
/**
* Returns the result of applying this predicate to {@code input}. This method is <i>generally
* expected</i>, but not absolutely required, to have the following properties:
*
* <ul>
* <li>Its execution does not cause any observable side effects.
* <li>The computation is <i>consistent with equals</i>; that is, {@link Objects#equal
* Objects.equal}{@code (a, b)} implies that {@code predicate.apply(a) ==
* predicate.apply(b))}.
* </ul>
*
* @throws NullPointerException if {@code input} is null and this predicate does not accept null
* arguments
*/
boolean apply(@Nullable T input);
/**
* Indicates whether another object is equal to this predicate.
*
* <p>Most implementations will have no reason to override the behavior of {@link Object#equals}.
* However, an implementation may also choose to return {@code true} whenever {@code object} is a
* {@link Predicate} that it considers <i>interchangeable</i> with this one. "Interchangeable"
* <i>typically</i> means that {@code this.apply(t) == that.apply(t)} for all {@code t} of type
* {@code T}). Note that a {@code false} result from this method does not imply that the
* predicates are known <i>not</i> to be interchangeable.
*/
@Override
boolean equals(@Nullable Object object);
}
里面只有一個apply方法,接收一個泛型的實參,返回一個boolean值。由于java世界中函數并不是一等公民,所以我們無法直接傳遞一個條件函數,只能通過Predicate這個類包裝一下。
And Predicate
如果要再實現一個方法來查找People列表中所有名字中包含b字母的列表,我們可以用 Guava 簡單的實現。
List<Person> namedPeople = newArrayList(filter(people, new Predicate<Person>() {
public boolean apply(Person person) {
return person.getName().contains("b");
}
}));
一切是這么的簡單。 那么新需求來了,如果現在需要找年齡>=20并且名稱包含b的人,該如何實現那? 可能你會這樣寫。
List<Person> filteredPeople = newArrayList(filter(people, new Predicate<Person>() {
public boolean apply(Person person) {
return person.getName().contains("b") && person.getAge() >= 20;
}
}));
這樣寫的話就有一定的代碼重復,因為之前我們已經寫了兩個Predicate來分別實現這兩個條件判斷,能不能重用之前的Predicate那?答案是能。 我們首先將之前生成年齡判斷和名稱判斷的兩個Predicate抽成方法。
private Predicate<Person> ageBiggerThan(final int age) {
return new Predicate<Person>() {
public boolean apply(Person person) {
return person.getAge() >= age;
}
};
}
private Predicate<Person> nameContains(final String str) {
return new Predicate<Person>() {
public boolean apply(Person person) {
return person.getName().contains(str);
}
};
}
而我們的結果其實就是這兩個Predicate相與。 Guava 給我們提供了and方法,用于對一組Predicate求與。
List<Person> filteredPeople = newArrayList(filter(people, and(ageBiggerThan(20), nameContains("b"))));
由于and接收一組Predicate,返回也是一個Predicate,所以可以直接作為filter的第二個參數。如果不熟悉函數式編程的人可能感覺有點怪異,但是習慣了就會覺得它的強大與簡潔。 當然除了and, Guava 還為我們提供了or,用于對一組Predicate求或。這里就不多講了,大家可以自己練習下。
Map(transform)
列表操作還有另一個常見的模式,就是將數組中的所有元素映射為另一種元素的列表,這就是map pattern。舉個例子,求People列表中的所有人名。程序員十有八九都會這樣寫。
List<String> names = new ArrayList<String>();
for (Person person : people) {
names.add(person.getName());
}
Guava 已經給我們提供了這種Pattern的結果辦法,那就是使用transform方法。
List<String> names = newArrayList(transform(people, new Function<Person, String>() {
public String apply( Person person) {
return person.getName();
}
}));
Function是另外一種用于封裝函數的接口對象。它的定義如下:
Function.java
@GwtCompatible
public interface Function<F, T> {
/**
* Returns the result of applying this function to {@code input}. This method is <i>generally
* expected</i>, but not absolutely required, to have the following properties:
*
* <ul>
* <li>Its execution does not cause any observable side effects.
* <li>The computation is <i>consistent with equals</i>; that is, {@link Objects#equal
* Objects.equal}{@code (a, b)} implies that {@code Objects.equal(function.apply(a),
* function.apply(b))}.
* </ul>
*
* @throws NullPointerException if {@code input} is null and this function does not accept null
* arguments
*/
@Nullable T apply(@Nullable F input);
/**
* Indicates whether another object is equal to this function.
*
* <p>Most implementations will have no reason to override the behavior of {@link Object#equals}.
* However, an implementation may also choose to return {@code true} whenever {@code object} is a
* {@link Function} that it considers <i>interchangeable</i> with this one. "Interchangeable"
* <i>typically</i> means that {@code Objects.equal(this.apply(f), that.apply(f))} is true for all
* {@code f} of type {@code F}. Note that a {@code false} result from this method does not imply
* that the functions are known <i>not</i> to be interchangeable.
*/
@Override
boolean equals(@Nullable Object object);
}
它與Predicate非常相似,但不同的是它接收兩個泛型,apply方法接收一種泛型實參,返回值是另一種泛型值。正是這個apply方法定義了數組間元素一對一的map規則。
reduce
除了filter與map模式外,列表操作還有一種reduce操作。比如求people列表中所有人年齡的和。 Guava 并未提供reduce方法。具體原因我們并不清楚。但是我們可以自己簡單的實現一個reduce pattern。 先定義一個Func的接口。
Func.java
public interface Func<F,T> {
T apply(F currentElement, T origin);
}
apply方法的第一個參數為列表中的當前元素,第二個參數為默認值,返回值類型為默認值類型。 然后我們定義個reduce的靜態方法。
Reduce.java
public class Reduce {
private Reduce() {
}
public static <F,T> T reduce(final Iterable<F> iterable, final Func<F, T> func, T origin) {
for (Iterator iterator = iterable.iterator(); iterator.hasNext(); ) {
origin = func.apply((F)(iterator.next()), origin);
}
return origin;
}
}
reduce方法接收三個參數,第一個是需要進行reduce操作的列表,第二個是封裝reduce操作的Func,第三個參數是初始值。
我們可以使用這個reduce來實現求people列表中所有人的年齡之和。
Integer ages = Reduce.reduce(people, new Func<Person, Integer>() {
public Integer apply(Person person, Integer origin) {
return person.getAge() + origin;
}
}, 0);
我們也可以輕松的寫一個方法來得到年齡的最大值。
Integer maxAge = Reduce.reduce(people, new Func<Person, Integer>() {
public Integer apply(Person person, Integer origin) {
return person.getAge() > origin ? person.getAge() : origin;
}
}, 0);
Fluent pattern
現在新需求來了,需要找出年齡>=20歲的人的所有名稱。該如何操作那?我們可以使用filter過濾出年齡>=20的人,然后使用transform得到剩下的所有人的人名。
private Function<Person, String> getName() {
return new Function<Person, String>() {
public String apply( Person person) {
return person.getName();
}
};
}
public void getPeopleNamesByAge() {
List<String> names = newArrayList(transform(filter(people, ageBiggerThan(20)), getName()));
}
這樣括號套括號的著實不好看。能不能改進一下那? Guava 為我們提供了fluent模式的API,我們可以這樣來寫。
List<String> names = from(people).filter(ageBiggerThan(20)).transform(getName()).toList();
Guava 中還有很多好玩的東西,大家時間可以多發掘發掘。這篇文章的源碼已經被我放置到 github 中,感興趣的可以自行查看。
來自: http://www.cnblogs.com/huang0925/p/3160094.html