Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering on List based on one property with guava

Tags:

java

guava

I have a class called Person -

public class Person implements Nameable {
    private String name;

    public String getName(){
        return name;
    }
}

Now I have two lists -

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

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.

How can I do this?

like image 692
Premraj Avatar asked Apr 26 '11 17:04

Premraj


1 Answers

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
                    )
                )
            );

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;
    }

}

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

like image 118
Ray Avatar answered Oct 19 '22 11:10

Ray