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

You can use intersection in an OCL expression to return the elements that two collections have in common; it is for modelers who need to filter a collection by membership in another collection.

Syntax

collection1->intersection(collection2)

The documented signature is:

intersection ( bag : Bag(T) ) : Bag(T)

collection1 is the collection on the left side of the operator. The bag parameter is the collection in parentheses. The result contains the elements that occur in both collections.

Find elements shared by two collections

Use intersection when the question is: Which of these objects are in both collections?

For example, assume that glassBowl.contains and allOranges are collections of Fruit objects:

glassBowl.contains->intersection(allOranges)

The result is the oranges that are in the glass bowl. If the glass bowl contains an orange and an apple, and allOranges contains every orange, the apple is not included because it is not a member of both collections.

A numeric example makes the membership rule explicit:

Left collection Right collection Expression Result
Set{0, 1, 3, 5} Set{0, 3, 7} Set{0, 1, 3, 5}->intersection(Set{0, 3, 7}) Set{0, 3}

0 and 3 remain because both occur on the left and right. 1 and 5 occur only on the left, while 7 occurs only on the right.

Membership is based on the same object

For collections of model objects, intersection compares membership of the object instances. The same object must be reachable through both collections to be returned.

For example, if the same Fruit object is in both bowl1.contains and bowl2.contains, it is included:

bowl1.contains->intersection(bowl2.contains)

In the collection examples, an object that is present in both input collections occurs once in the result. Adding the same object to an input collection again does not create another shared object in the displayed result.

Choose the correct collection operation

If you need to find... Use Example
Elements in both collections intersection teamA->intersection(teamB)
Elements in the left collection but not the right collection difference teamA->difference(teamB)
Elements that occur in either collection union teamA->union(teamB)
Elements that occur in one collection or the other, but not both symmetricDifference teamA->symmetricDifference(teamB)

The order matters for difference, but intersection asks for common membership, so swapping the two collections does not change which elements they have in common.

Create a collection for testing

Use Set literals when you want to test an expression with fixed values. A Set contains no duplicates.

Set{14, 22, 12}->intersection(Set{12, 22, 53})

This expression returns the shared values 12 and 22.

For additional collection-operator examples, see Examples on collection operators. You can also watch the intersection walkthrough.

See also