🚀 Welcome to MDriven Learn –  Don’t miss the latest Release Notes.
OCL Collection Operators

Once you have a collection of objects, there are certain operators applicable to them.

You can use the OCL-Editor to see what they are:

Operators Description
->append Add another object at the end
->asBag Returns a Bag containing all elements of self.
->asSequence Returns a Sequence containing all elements of self. Element ordering is preserved when possible.
->asSet Returns a Set containing all elements of self.
->at Get the objects at X, where the first index is 1
->at0 Get the objects at X, where the first index is 0
->collect Returns a collection containing the result of applying expr on all elements contained in self.
->count Count how many meet a certain criterion
->dictionary Efficiently looks up values
->difference The difference between 2 collections
->excluding The collection except for this single object
->exists Returns true if at least one element in self validates the condition expr, false otherwise.
->filterOnType Only keep the ones of a certain type
->first Return the first object
->forAll Returns true if all the elements contained in self validate the condition expr, false otherwise.
->groupBy Build a collection of tuples grouped by some aspect
->includes Does the collection include the object?
->includesAll Does the collection include the whole other collection?
->including Returns the list with the element in the parameter included.
->IndexOf The 1-based index of an object in the collection, possibly -1 if it does not exist
->indexOf0 The 0-based index of an object in the collection, possibly -1 if it does not exist
->intersection The intersection of two collections
->isEmpty Returns true if the collection is empty
->notEmpty Returns true if self contains at least one element, false otherwise.
->reject Returns a collection with all elements of self except for those who validate the OclExpression expr.
->select Returns a collection with all elements of self that validate the OclExpression expr.
->size Returns the number of elements contained in self.
->sum Returns the sum of all elements contained in self if they support the '+' operation.
->last Returns the last object in the collection
->orderBy Sorts the collection on one or more properties
->orderDescending Sort from biggest to smallest
->orderGeneric Sorts the list of properties with interchangeable sort order: (expr1, OclSortDirection::ascending, expr2, OclSortDirection::descending...)
->prepend Returns an OrderedSet containing object followed by all elements of self.
->subsequence Returns a smaller collection from a start to a stop
->symmetricDifference The symmetric difference between the collections; i.e., all the objects in collection1 or collection2 but not in both
->union The set of objects in collection1 and in collection2

See this page for examples on collection operators.

Also check out: OCLOperators