我敢肯定,有一種更簡單的方法...爲了比較起見,下面的內容會將人物變爲名稱。對於subsetOfPersons
,我們實際上直接創建了一個名稱列表,因爲這是我們真正需要的。對於persons
,我們將轉換限制在比較的上下文中。
Iterable<Person> filtered = Iterables
.filter(
persons,
Predicates.not(
Predicates.compose(
Predicates.in(ImmutableSet.copyOf(Iterables.transform(subsetOfPersons, personToNamefunction))),
personToNamefunction
)
)
);
編輯:想你可能欣賞一個JUnit:再次
package com.stackoverflow.test;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.Test;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
public class PersonTest {
public class Person {
private String name;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Test
public void testNameBasedFiltering() {
Person bob = createPerson("bob");
Person jim = createPerson("jim");
Person pam = createPerson("pam");
Person roy = createPerson("roy");
ImmutableList<Person> persons = ImmutableList.of(
bob,
jim,
pam,
roy);
ImmutableList<Person> subsetOfPersons = ImmutableList.of(
createPerson("jim"),
createPerson("pam"));
Function<Person, String> personToNamefunction = new Function<Person, String>() {
public String apply(Person arg0) {
return arg0.getName();
}
};
Iterable<Person> filtered = Iterables
.filter(
persons,
Predicates.not(
Predicates.compose(
Predicates.in(ImmutableSet.copyOf(Iterables.transform(subsetOfPersons, personToNamefunction))),
personToNamefunction
)
)
);
for (Person person : filtered) {
assertNotSame(jim, person);
assertNotSame(pam, person);
}
}
public Person createPerson(String name) {
Person person = new Person();
person.setName(name);
return person;
}
}
編輯:錯過了 「不」 要求的第一次。易於修復 - 帶謂詞,你可以包裝Predicates.not(..)
!
來源
2011-04-26 17:36:49
Ray
甚至沒有想到Predicates.compose,好! – sjr 2011-04-26 17:44:16
我想他不想(在(nameSet)) – sjr 2011-04-26 17:45:01
感謝您的召集。用not包裝謂詞,並更新JUnit以反映變化。 – Ray 2011-04-26 17:50:23