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

You can use count in OCL to return how many times a specified value occurs in a collection.

Syntax

collection->count(value)

count has the following signature:

count(object : T) : Integer

It returns an Integer: the number of elements in self that equal the supplied value.

Count matching values

Use count after an expression that produces a collection of values. For example, this expression counts customers whose Name value is 'John':

Customer.allInstances.Name->count('John')

The expression is evaluated as follows:

Expression part Result
Customer.allInstances The collection of all Customer objects.
.Name The collection of Name values from those customers.
->count('John') The number of Name values equal to 'John'.

For example, if the customer names are 'John', 'Ana', 'John', and 'John', the expression returns 3.

Count versus size

Use count(value) when you need the number of occurrences of one particular value. Use size when you need the total number of elements in a collection.

Requirement Expression Example result
Count all customer names in the collection Customer.allInstances.Name->size() 4 for four customers
Count only names equal to 'John' Customer.allInstances.Name->count('John') 3 when three names are 'John'

See also