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

You can choose a Set or Bag in OCL when you need an unordered collection, depending on whether your expression must retain duplicate elements.

Choose the collection type

A collection groups values or objects so that you can evaluate them together. Both Set and Bag are unordered collections, but they treat repeated elements differently.

Type Duplicate elements Use it when Example
Set Does not retain duplicates. Each distinct element occurs once. You need unique values or unique objects. Set{1,2,2,3} contains 1, 2, and 3.
Bag Retains duplicates. The same element can occur more than once. You need each occurrence to remain in the result, for example when processing repeated input values. Bag{1,2,2,3} contains two occurrences of 2.

Create a Set or Bag

Use the collection type name followed by braces.

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

The difference becomes visible when the input contains a repeated value:

Set{'Email','Email','Phone'}
Bag{'Email','Email','Phone'}

The Set has one 'Email' value. The Bag has two 'Email' values.

Work with the elements

Use ->collect(...) to evaluate an expression for each element. This example invokes an expression for every integer in the collection:

Set{14,22,12}->collect(i | DoSomthingWithAnInt(i))
Bag{14,22,12}->collect(i | DoSomthingWithAnInt(i))

When a Bag contains duplicates, the collection has repeated occurrences available to the operation. For example, the Bag below includes 2 twice:

Bag{1,2,2,3}->collect(i | DoSomthingWithAnInt(i))

Convert an existing value or collection

Use asBag() when you need a Bag. It wraps a single object in a one-element Bag, or converts a collection while retaining duplicate elements.

Use asSet() when you need uniqueness. Converting a collection to a Set removes repeated occurrences of the same element.

For example, convert a Bag with repeated values to a Set when subsequent logic must work with each value only once:

Bag{1,2,2,3}->asSet()

The result contains 1, 2, and 3.

Combining collections

The result type and duplicate behavior also depend on the operator and the collection types you combine.

  • union combines two collections. A union of Bags retains all occurrences; a union of Sets retains unique elements.
  • intersection returns elements shared by the collections.

For example, a Bag union can retain a repeated value:

Bag{'a','b'}->union(Bag{'a','c'})

The result contains 'a' twice. Use Sets instead when the result must contain each value only once.

Order and mutability

Neither Set nor Bag defines an element order. If your expression requires a collection in a specified order, use Sequence instead.

Raw-data OCL collections are immutable: you do not change the existing Set or Bag. Transform the collection with an OCL expression to produce a new collection. See Documentation:Collections for the distinction between immutable raw-data collections and mutable class-object collections in the Action Editor.

See also