File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cross Reference two ArrayList Duplicates Removal

 
Michael Labuschagne
Ranch Hand
Posts: 56
Java Tomcat Server Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two ArrayLists containing objects of the same type in both... You want to process the two lists and remove duplicates (i.e. where an object is deemed to be the same as another object in the other ArrayList)... The objects in the individual ArrayLists do not contain duplicates and are sorted according to primary key (which is one of the test fields to determine two objects are the same - i.e. an object from list 1 with primary key 'PK1' could have a 'sister' element list 2 which also has primary key 'PK1' but due to other member values having different values would be deemed to not be equal) the objects have a method isIdentical(object) which determines whether the two are identical.

So far I have come up with a crude and inefficient method of removing duplicates:



This has many, many iterations through the array structures is there not a better method? Any suggestions are welcome!
 
Jayesh A Lalwani
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apache commons collections has a utility class called CollectionUtils that can do that for you
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use Guava's colletions, specifically put the values in a set, and use the difference or symmetric difference methods. If its fast enough for Google, I'm good.

 
Winston Gutkowski
Bartender
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Labuschagne wrote:You have two ArrayLists containing objects of the same type in both... You want to process the two lists and remove duplicates (i.e. where an object is deemed to be the same as another object in the other ArrayList)... The objects in the individual ArrayLists do not contain duplicates and are sorted according to primary key (which is one of the test fields to determine two objects are the same - i.e. an object from list 1 with primary key 'PK1' could have a 'sister' element list 2 which also has primary key 'PK1'...

From your description, I suspect you could use a 'stepping' comparison of the two lists based on matching PKs. However, if it was me, I think I'd just load one of the lists (probably the larger one) into a HashSet and run contains().

However, in order for that to work, you'll have to make your equals() method do what isIdentical()(*) does currently, and also add a decent hashCode() method.

Winston

(*)Edit: BTW, that's not a great name for a method in my opinion. To me, "identical" means "has the same reference" (ie, obj1 == obj2).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic