Envin
Envin

Reputation: 1523

Comparing an element in two different List that are different objects

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?

Upvotes: 1

Views: 1391

Answers (3)

Brendan Long
Brendan Long

Reputation: 54242

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.

Upvotes: 2

Miserable Variable
Miserable Variable

Reputation: 28752

foo.contains(j.getId()))

foo is List<AbstractA> and j.getId() is (I guess) a String. Since List.contains uses the equals method this will never be true unless you define AbstractA.equals in a strange way.

Best would be to write your own method to iterate through the list and compare. You can use Guava but it would be overkill for just this

Upvotes: 1

Kent
Kent

Reputation: 195049

You may want to have two Maps instead of Lists.

for foo:

key: id
value: Object of AbstractA

for names:

key: id
value: Name object

then you could compare the keys (id in your case)

I hope I understood you right.

Upvotes: 0

Related Questions