You can use difference in an OCL expression to return the members of one collection that are not present in another collection; it is for developers comparing collections in MDriven expressions.
Syntax
collection1->difference(collection2)difference subtracts collection2 from collection1. The result contains the members that occur in the first collection and do not occur in the second collection.
The order of the operands matters:
collection1->difference(collection2)means collection1 minus collection2. It does not return items that exist only in collection2.
Examples
| Expression | Result | Why |
|---|---|---|
Set{'a','b','c','d'}->difference(Set{'b','c','d','e'})
|
Set{'a'}
|
'a' is in the first set and not in the second.
|
Set{'a','b','c','d','e'}->difference(Set{'b','c','d','e'})
|
Set{'a'}
|
All other members of the first set also occur in the second set. |
Set{'a','b','c','d','e','f'}->difference(Set{'b','c','d','e'})
|
Set{'a','f'}
|
'a' and 'f' are not present in the second set.
|
Set{'a','b','c','d','e','f'}->difference(Set{'a','b','c','d','e','f'})
|
empty collection | Every member of the first set is removed. |
Set{'a','b','c','d','e','f'}->difference(Set{'a','b','c','d','e','f','g'})
|
empty collection | The extra 'g' in the second set has no effect because it is not in the first set.
|
Set{'a','b','c','d','e','f','g'}->difference(Set{'a','b','c','d','e','f'})
|
Set{'g'}
|
'g' remains because the second set does not contain it.
|
Operand order
Reverse the collections when you need to find what exists in the other collection but not in the first one.
Set{'a','b','c','d'}->difference(Set{'b','c','d','e'})
-- Result: Set{'a'}
Set{'b','c','d','e'}->difference(Set{'a','b','c','d'})
-- Result: Set{'e'}In the first expression, 'a' survives because it is only in the first set. In the second expression, 'e' survives for the same reason.
Example with object collections
Use difference when two collections contain the same kind of objects and you want the objects remaining after removing the members of another collection.
For example, if ThingOnes contains objects numbered 1, 2, and 5, and ThingTwos contains 2 and 5:
ThingOnes->difference(ThingTwos)returns the object numbered 1. If the object numbered 5 is removed from ThingTwos, it is no longer subtracted and therefore appears in the result together with 1.
Difference compared with symmetric difference
Use difference when you want the remainder of the first collection after removing the second collection.
Use symmetricDifference when you want members that occur in either collection but not in both. Symmetric difference considers both collections; difference considers only what remains from the first collection.
For example:
Set{'a','b','c'}->difference(Set{'b','c','d'})
-- Result: Set{'a'}
Set{'a','b','c'}->symmetricDifference(Set{'b','c','d'})
-- Result: Set{'a','d'}Walkthrough
See the OCL Operators: difference walkthrough for a live example that removes selected objects from two collections and observes how the result changes.
