基于番石榴的一种特性对列表进行过滤 [英] Filtering on List based on one property with guava

查看:71
本文介绍了基于番石榴的一种特性对列表进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做Person的课程-

I have a class called 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

现在我想过滤subsetOfPersons中不存在的persons,相等标准是name属性,Person没有相等.

Now I would like to filter the persons which are not present in the subsetOfPersons, equality criteria is name property and Person doesn't have equals.

我该怎么做?

推荐答案

我敢肯定有一种更简单的方法...为了进行比较,下面将把人转换为姓名.对于subsetOfPersons,我们实际上直接创建了一个名称列表,因为这是我们真正需要它们的全部.对于persons,我们将转换限制在比较的上下文中.

I'm sure there's a simpler way... the below would transform person to name for the sake of comparison. For the subsetOfPersons, we actually create a list of names directly, since that's all we really need from them. For the persons, we keep the transformation limited to the context of the comparison.

    Iterable<Person> filtered = Iterables
            .filter(
                persons, 
                Predicates.not(
                    Predicates.compose(
                        Predicates.in(ImmutableSet.copyOf(Iterables.transform(subsetOfPersons, personToNamefunction))),
                        personToNamefunction
                    )
                )
            );

编辑:以为您可能会喜欢JUnit:

Edit: Thought you might appreciate a 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;
    }

}

再次编辑:首次缺少"not"要求.易于修复-有了谓词,您就可以用Predicates.not(..)包装!

Edit again: Missed the "not" requirement the first time. Easy fix--with predicates, you can just wrap with Predicates.not(..)!

这篇关于基于番石榴的一种特性对列表进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆