• 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

Comparing two Collections for "equality"

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two Collections A and B. Both contain the same object types.
I want to compare both Collection for equality. This is how I am
doing it now. Pseudocode follows:

Does anybody see any holes in the logic / code above ?
Thanks all
Gavin
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are assuming that all the objects have an equals method defined? (this assumption is ok, I just want to state it.)
You may run into trouble if the collections contain duplicates. For example if collection a is [X,X,Y] and collection b is [X,Y,Y], your code will not catch the difference.
If the collections can't contain duplicates, the second check for containsAll is redundant.
 
Gavin Bong
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,
Thanks for your observations.
The parameters "can" and "may" contain duplicates.
I wonder whether I can just simplify it by doing

Gavin
[ October 01, 2003: Message edited by: Gavin Bong ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic