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

  • SEARCH
  • Home
  • 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 7681351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:19:19+00:00 2026-05-31T18:19:19+00:00

Hello I have 2 lists that contains the same objects. I would like to

  • 0

Hello I have 2 lists that contains the same objects. I would like to perform any operation like intercesct, union, distinct by using predicate because I am unable to use equals to comparision.

Example:

class Car{
  public String id;
  public String color;
  public int hashcode(){
    //id field is used for hashcode
  }
  public boolean equals(){
    //id field is used for equals
  }
}

Now I have two lists of Cars. I need to find duplicates in this lists but not by id only by color.

List<Car> carList1 = new ArrayList(){ new Car(1,blue), new Car(2,green)};
List<Car> carList2 = new ArrayList(){ new Car(1,silver), new Car(4,green)};

I need to find second object from carList1 (new Car(2,green))

List Something similar to

Collection.intersect(carList1,carList2,comparator).

In C# I would use for it LINQ.

  • 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-31T18:19:21+00:00Added an answer on May 31, 2026 at 6:19 pm

    You can do similar think using Guava.

    1) intersect is operation on sets, not on lists. So you should construct them like

    final Set<Car> first = ImmutableSet.of( new Car(1, "blue"), new Car(2, "green") );
    

    or, if you need special comparator ( predicate mentioned )

    final Set<Car> second = newTreeSet( new Comparator<Car>(){
        public int compare( final Car o1, final Car o2 ){
            return o1.getColor().compare( o2.getColor() );  //return 0 when predicate return true
        }
    } );
    second.add( new Car(1, "green")  );
    

    UPD: You should use only one way to construct both sets.

    Than call intersection

     final Set<Car> intersection = Sets.intersection( first, second );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have this list that i am working on. When i move the
hello friends i have a check box list which contains all the courses kept
I have a property that contains multiple values, and I want to execute a
I have program that needs to parse XML that contains character entities. The program
Hello I have a little problem with assigning property values from one lists items
Hello I am analyzing some data and trying to use a package that contains
I have a list with class objects say List a[10]; class Hello extends abc
I have a main menu screen with a simple ListView that contains links to
Hello i have a list view control, While the form is being loaded i
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc

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.