Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4542020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:19:46+00:00 2026-05-21T15:19:46+00:00

I have a class called Person – public class Person implements Nameable { private

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:19:47+00:00Added an answer on May 21, 2026 at 3:19 pm

    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(..)!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class called Person: public class Person { private String name,
I have this class called Table: class Table { public string Name { get
I have a class called Book; class Book { public string Name { get;
I have a string like (Name := Sam&&Age=:17&& Score:=C6) and a class called Person
I have one class called Person that basically looks like: public class Person {
I have an java interface public interface IPerson { Person addPerson(String name ); Person
I have a class called Person, which contains various properties such as first name,
Im having rather an annoying problem. I have a class called Person and a
I have an ORM class called Person, which wraps around a person table: After
I have Class called Person containing to properties, Father and List of Children. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.