基于一个使用番石榴的属性过滤List

时间:2011-04-26 17:11:50

标签: java guava

我有一个名为Person的课程 -

public class Person implements Nameable {
    private String name;

    public String getName(){
        return name;
    }
}

现在我有两个清单 -

List<Person>  persons = // some persons
List<Person> subsetOfPersons = // some duplicate persons, but different objects and don't share the same identity

现在我想过滤persons中不存在的subsetOfPersons,相等标准是name属性而Person不具有等号。

我该怎么做?

2 个答案:

答案 0 :(得分:9)

我确信有一种更简单的方法......为了便于比较,下面会将人变为名字。对于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(..)包裹!

答案 1 :(得分:0)

似乎你必须手动迭代两个列表(听起来很蹩脚,但这是我唯一能想到的)。

outer loop: persons
   inner loop: subsetOfPersons
       compare person and sub person names and create another list with intersection of the      two