You use ->reject() in OCL to return the elements of a collection that do not meet a Boolean condition.
Syntax
collection->reject(iterator | condition)| Part | Meaning |
|---|---|
collection
|
The source collection, also called self in the operator definition.
|
iterator
|
A temporary name for each element while the condition is evaluated. |
condition
|
A Boolean expression. Elements for which this expression is true are removed from the returned collection.
|
The operator signature is:
reject(expr : OclExpression) : Collection(T)Filter a collection
The following expression starts with four integers and rejects values less than 2:
Set{1, 2, 3, 4}->reject(number | number < 2)The result contains 2, 3, and 4. The value 1 is excluded because number < 2 evaluates to true for that element.
reject keeps an element when its condition evaluates to false. In this example, 2 < 2 is false, so 2 remains in the result.
Write the condition
Use a condition that evaluates to true for the elements you want to remove. For example, to remove empty text values from a collection of strings, the condition must identify the values considered empty:
texts->reject(text | text = '')Use the Boolean expressions described in OCL Boolean Operators when you need to combine conditions. For example, a condition can test more than one property of each object.
Result behavior
- The result is a collection of the same element type
Tas the source collection. - An element is removed only when the condition is
truefor that element. - If no elements meet the condition, the result contains all source elements.
- If every element meets the condition, the result is empty.
- A Set has no duplicate values; use it when you need a literal collection without duplicates.
