OCLOperators union
Created by Alexandra on 2017-08-14 · Last edited by Sandra.akech on 2025-12-12.
union ( bag : Bag(T) ) : Bag(T)
Returns a Bag containing all elements of self and all elements of *bag*.
| Expression | Result |
|---|---|
| Bag{'a', 'b', 'a'}->union(Bag{'b', 'c'}) | Bag{'a', 'b', 'a', 'c'} |
| Set{1,2,3}->union(Set{2,3,4}) | Set{1,2,3,4} |
Video: https://www.youtube.com/watch?v=-vBshXtEPts
Example 1: Bags
Expression:
Bag{'a', 'b', 'a'}->union(Bag{'b', 'c'})Result:
Bag{'a', 'b', 'a', 'c'}- All elements from both Bags are included.
'a'appears twice because Bags allow duplicates.
Example 2: Sets
Expression:
Set{1,2,3}->union(Set{2,3,4})Result:
Set{1,2,3,4}- All unique elements from both Sets are included.
- Duplicates are automatically removed.
