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 8804189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:32:20+00:00 2026-06-14T01:32:20+00:00

I will post my code, but just change the names. I’ll add comments when

  • 0

I will post my code, but just change the names. I’ll add comments when I can add more info.

List<AbstractA> foo = bar.getFoo; // This returns an ArrayList<E> with two objects. Each object has an ID and Price. 

List<Name> names = null;
try{
   names = someClass.getNames(); // This returns an ArrayList<E> with 10 Name objects. Each one has an ID, name, description
}catch(Exception e){
   Log.warn(e);
}

My main goal is compare the two lists. I have…

Iterator<Name> object = names.iterator();
while(object.hasNext()){
   Name j = object.next(); // assign next name
   System.out.println("j.getId(): " + j.getId()); // This provides me the Id
   System.out.println("foo.contains(j.getId()) " + foo.contains(j.getId())); // Keeps spitting out false but I want it to be true

   if(foo.contains(j.getId())){
      object.remove(); //remove name out of Names list
   }
}

I’m not sure if this makes a lot of sense of what I am trying to do.
There are two beans in this program representing foo and name. So they are different objects and I think that may be the issue.

Any suggestions? Sorry if this is VERY vague…

My main question is, if I want to compare an element in these two Lists, what is the best way to 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-06-14T01:32:21+00:00Added an answer on June 14, 2026 at 1:32 am

    List.contains(…) uses equals() for its comparisons:

    More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

    equals() doesn’t require the two objects to be the same class, so you can override it like this:

    class Name {
    
        // Stuff
    
        @Override
        bool equals(Object other) {
            if(other instanceof Name) {
                Name otherName = (Name)other;
                // Compare this and otherName, return true or false depending
                // on if they're equal
            } else if (other instanceof AbstractA) {
                AbstractA otherAbstractA = (AbstractA)other;
                // Compare this and otherAbstractA, return true or false depending
                // on if they're equal
            } else {
                return false;
            }
        }
    }
    

    You probably want to override equals() for both, so that a.equals(b) == b.equals(a).

    If you’re finding yourself doing this a lot, it may be that an abstract class that they both implement will help.

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

Sidebar

Related Questions

I will post my code below as a quick note I define both of
My code will process data in batches as recommended in this this post and
I'm trying to add a feature to my app that will post a status
I don't know if more information is needed than the code below, but if
I will post two examples, one that validates and one that doesn't. I am
I need to write a script which will post username and password onto a
I have created a Webpage which will post as post method..not as get method.
Im trying to create a script that will post a notification stored in a
I am trying to run a system call which will post an activity to
I need to create a script where someone will post an opening for a

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.