-1
CompareList removedList = new CompareList();
CompareList addedList = new CompareList();

This is how I'm adding elements inside

addedList.add(new Objec("Var_a", "1"));

and class Objec has two strings.

How can I compare that? I can't use contains like I could with ArrayList of Strings?

In CompareList I have

public boolean equals(CompareList l) {

    if (l.containsAll(this)) {
        if (this.containsAll(l)) {
            return true;
        }
    }
    return false;
}

and in Objec

public Objec(String n, String s) {
    this.name=n;
    this.surname=s;
}

public String toString() {
    return " Name: " + name +  ", Surname: " + surname;
}

I see that many people are confused with my question. So what I want?

List 1:

Samy Joe
Emma Than
Julia Rob

List 2:

Samy Joe
Emma Than
Anna Sky

Removed Julia Rob and added Anna Sky. But I don't know how to do it when my lists contains of object that have two strings?

gawi
  • 2,843
  • 4
  • 29
  • 44
Alen
  • 133
  • 2
  • 9

3 Answers3

3

This piece of code compares if the lists are equal, that is, contains the same elements.

static boolean same(Collection<?> a, Collection<?> b) {
    if (a.size() != b.size()) {
        return false;
    }
    List<?> c = new ArrayList<>(a);
    c.removeAll(b);
    return c.isEmpty();
}
  • If the sizes are not equal, then the lists are never equal.
  • Else, if the sizes are equal, then we know that both lists contain one or more elements that are not present in the other list. So we make a new list from one of the lists (list a in my case), and then we remove the elements of b.

You don't need to use your own class CompareList, instead you could just use an ArrayList or something.

In order to compare your Objec to another one, you'll need to implement equals(Object) and hashCode() correctly.


If you want to know which elements are not contained in the other list, then you can use this:

static HashMap<Collection<?>, Collection<?>> disjoints(Collection<?> a, Collection<?> b) {
    List<?> aa = new ArrayList<>(a);
    aa.removeAll(b);
    List<?> bb = new ArrayList<>(b);
    bb.removeAll(a);

    HashMap<Collection<?>, Collection<?>> map = new HashMap<>();
    map.put(a, aa);
    map.put(b, bb);
    return map;
}

It returns a map with as keys the two collections and as values the elements of the collection specified by the key, which are not contained in the other collection. For example, if you want to know the elements of a not present in b, then call disjoints(a, b).get(a).

Note: I call the lists collections, because they are. In Java, a List is a subtype of Collection.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

You need to override the equals method in your custom object like this:

public class MyObject {
     private String name;
     private String surname;

    @Override
    public boolean equals(MyObject myObject) {
         // assert that name and surename can not be null or check for this
         if (!this.name.equals(myObject.name)) {
             return false;
         }

         if (!this.surname.equals(myObject.surname)) {
             return false;
         }

         return true;
    }
}

The contains-Method of a list will be use the equals method to check the equality of your objects.

But the link that @Prakash has posted is the better way do do that. I think no one understand what your attention is to do with yout CompareList and why you use a custom one ;)

EDIT: Sorry, had mistake in the Signature.

@Override
    public boolean equals(Object obj) {
        if (!(obj instanceof MyObject) {
            return false;
        }
        // else cast and use code above
    }
d3rbastl3r
  • 463
  • 1
  • 6
  • 16
1

You're question: "How can I compare that?"

The first thing to keep in mind is that you can compare in 2 ways. so think about that first.

Equality: When comparing for equality

use equals() and while you're at it implement hashCode().

Comparing / sorting: When are 2 objects considered lower then, same, or higher then the other?

implement "Comparable" interface and override compareTo.

You're post contains the equals() so lets say that is what you want to do. If you want to use containsAll(), keep in mind that it uses equals from the class the list contains to figure out to return true/false. So you should probably:

add equals() to your "Objec" class.

Sven Dhaens
  • 1,916
  • 1
  • 15
  • 10