🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators Set
This page was created by Hans.karlsen on 2017-11-08. Last edited by Wikiadmin on 2026-07-29.

A Set is an unordered OCL collection with no duplicate elements; use it when you need a distinct group of values in an expression.

Create a Set

Use Set{...} and place the values between the braces.

Set{14, 22, 12, 53, 2, 11, 66}

This expression creates a Set of integers. You can also create a Set of strings:

Set{'String', 'EmailAddress'}

Duplicate values are removed

A Set retains each distinct value once. For example, this expression produces a Set containing 14, 22, and 53:

Set{14, 22, 14, 53, 22}

Do not use a Set when repeated values have meaning. Use a Bag for a collection that can contain duplicates.

Use a Set with collection operations

You can use a Set as the source collection for OCL collection operations. For example, collect evaluates an expression for every integer in this Set:

Set{14, 22, 12, 53, 2, 11, 66}->collect(i | DoSomethingWithAnInt(i))

Here, i is the current integer while the collect expression is evaluated. The called expression must be valid in the context where you use it.

The same pattern applies to strings:

Set{'String', 'EmailAddress'}->collect(s | DoSomethingWithAString(s))

Choose the right collection type

Use When you need Example
Set Distinct values; element order is not defined. Set{'New', 'Approved', 'Closed'}
Bag A collection in which duplicate values are retained. Bag{'New', 'New', 'Closed'}
Sequence A collection whose order matters, such as a numeric range for iteration or numbering. Sequence{0..20}

For example, use Sequence{0..20} rather than a Set when your expression depends on a number sequence in order. See Sequence for range syntax and ordered collections.

Convert an existing value or collection

Use asSet when you already have a value or collection and need a Set. Applied to a collection, asSet() removes duplicates; applied to a single value, it creates a Set containing that value.

For example:

someCollection->asSet()

Work with Set values

Use difference to subtract the elements of one collection from another. For example, the result contains only 'a':

Set{'a', 'b', 'c', 'd'}->difference(Set{'b', 'c', 'd', 'e'})

To obtain all currently existing instances of a classifier as a Set, use allInstances. To test whether a Set has no elements, use Empty.

See also