1

I have two Lists, say listA and listA1, listA contains object of class A type and listA1 also contains object of type class A.But listA added class A objects by java code and listA1 adds class A objects by converting JSON String to Class A.

Now I need to compare listA and listA1. If listA contains any objects which also are in listA1 I need to remove that object and return back listA with all the objects which are not in listA1.

How can I achieve this in Java?

GingerHead
  • 8,130
  • 15
  • 59
  • 93
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • @zander I used contains() method which is working fine when objects are same its not comparing values.. – Subodh Joshi Oct 11 '12 at 04:57
  • 2
    First of all Class A must have a "valid" equals implementation, then we can use [List.removeAll](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/List.html#removeAll%28java.util.Collection%29) – white Oct 11 '12 at 04:58
  • 1
    Implement a custom(your own) equals method for class A and use this method to compare and remove the objects using the method suggested by @white – krishnang Oct 11 '12 at 04:59
  • 1
    Read http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java, implement equals and hashCode – Jayan Oct 11 '12 at 05:00

4 Answers4

1

Please implement(override) equals method as below on your class A. Once done, contains method should help.

  public boolean equals(A a){
       boolean bEqual = false;
       if(this.value1!= null && a.value1!= null && 
          this.value1.eqauls(a.value1)
          ........
          ........
        ) {
          bEqual = true;              
        }
        return bEqual;
   }

EDIT:

Iterate elements of listA1. If they are present in listA, remove them.

   for(A a: listA1){
     if(listA.contains(a)){
        listA.remove(a);
     }
   }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • listA contains 15 element(Class A's object) and listA1 contains 5 elements(Class A's object) now i have to compare values of objects(listA and listA1) and remove all the objects from listA and which are also present in listA1.Thanks – Subodh Joshi Oct 11 '12 at 05:18
  • Thanks Yogendra i am doing same for (A : listA) { if (listA.contains(A)) { listA.remove(value); } } but that not working may be the addres of objects are diffrent because contains not checking values its checking address of object .its my thinking – Subodh Joshi Oct 11 '12 at 05:27
  • If it works for you, please don't forget to accept to close this question thread. – Yogendra Singh Oct 11 '12 at 05:28
  • Did you implement the equals method (comparing field values) in your class `A`? – Yogendra Singh Oct 11 '12 at 05:35
  • Please share you class `A` attributes with their data type. I will help you implement the `equals` method. – Yogendra Singh Oct 11 '12 at 05:53
  • Thanks i used your solution and i posted what exact code i did . – Subodh Joshi Oct 11 '12 at 06:05
0

Apache's commons.collections library has a CollectionUtils class that provides easy-to-use methods for Collection manipulation/checking, such as intersection, difference, and union.

The org.apache.commons.collections.CollectionUtils API docs are here.

I think, You can use intersaction methos for that

Tarun Gupta
  • 1,232
  • 2
  • 13
  • 26
0

If you are planning to compare two objects, then see the comparing objects

also, How it works equals

subodh
  • 6,136
  • 12
  • 51
  • 73
0

What exact thing i did to resolve the issue.....

 public static class A  {


        String  headerName;

        public String getHeaderName() {
            return headerName;
        }
        public void setListBoxHeaderName(String headerName) {
            this.headerName= headerName;
        }
        public A(String headerName) {
            super();

            this.headerName = headerName;
        }


        public boolean equals(A rData) {
            boolean bEqual = false;
            if (this.getHeaderName() != null && rData.getHeaderName() != null
                    && this.getHeaderName().equals(rData.getHeaderName())) {
                bEqual = true;
            }
            return bEqual;
        }

    }

And for comparing two list the code is as follows...

for (A value1 : listA1) {
            for (A value2: listA) {
                if (value2.equals(value1 )) {
                 listA.remove(value2);
                    break;
                }
            }
        }

This is what the exact solution i used Thanks

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202