• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cross Reference two ArrayList Duplicates Removal

 
Ranch Hand
Posts: 56
Tomcat Server Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache commons collections has a utility class called CollectionUtils that can do that for you
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic