• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Cross Reference two ArrayList Duplicates Removal  RSS feed

 
Ranch Hand
Posts: 56
Java Tomcat Server Windows
  • 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!
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • 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
 
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • 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.

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • 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).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!