# Bag(T)

## `Bag(T)`

A bag is a collection with duplicates allowed. That is, one object can be an element of a bag many times. There is no ordering defined on the elements in a bag. Bag is itself an instance of the metatype BagType.

conformsTo `Collection(T)`

Operations

`=(object2 : OclSelf[?]) : Boolean[1]` precedence: `EQUALITY`

True if `self` and bag contain the same elements, the same number of times.

`<>(object2 : OclSelf[?]) : Boolean[1]` precedence: `EQUALITY`

`excluding(object : OclAny[?]) : Bag(T)`

The bag containing all elements of `self` apart from all occurrences of object.

`excludingAll(objects : Collection(OclAny)) : Bag(T)`

The bag containing all elements of `self` apart from all occurrences of all objects.

`flatten(T2)() : Bag(T2)`

Redefines the Collection operation. If the element type is not a collection type, this results in the same bag as `self`. If the element type is a collection type, the result is the bag containing all the elements of all the recursively flattened elements of `self`.

`including(object : T[?]) : Bag(T)`

The bag containing all elements of `self` plus object.

`includingAll(objects : Collection(T)) : Bag(T)`

The bag containing all elements of `self` and objects.

`selectByKind(TT)(type : TT[?]) : Bag(TT)`

`selectByType(TT)(type : TT[?]) : Bag(TT)`

Iterations

`closure(i : T[1] | lambda : Lambda T() : Set(T)[?]) : Set(T)`

The closure of applying body transitively to every distinct element of the source collection.

`collect(V)(i : T[?] | lambda : Lambda T() : V[?]) : Bag(V)`

`collectNested(V)(i : T[?] | lambda : Lambda T() : V[?]) : Bag(V)`

The Bag of elements which results from applying body to every member of the source nonordered collection.

`reject(i : T[?] | lambda : Lambda T() : Boolean[1]) : Bag(T)`

The sub-bag of the source bag for which body is `false`.

`self->reject(iterator | body) = self->select(iterator | not body)
`

.

`select(i : T[?] | lambda : Lambda T() : Boolean[1]) : Bag(T)`

The sub-bag of the source bag for which body is `true`.

`self->select(iterator | body) =
self->iterate(iterator; result : Bag(T) = Bag{} |
if body then result->including(iterator)
else result
endif)
`

`sortedBy(i : T[?] | lambda : Lambda T() : OclAny[?]) : Sequence(T)`

Results in the Sequence containing all elements of the source collection. The element for which body has the lowest value comes first, and so on. The type of the body expression must have the < operation defined. The < operation must return a Boolean value and must be transitive (i.e., if a < b and b < c then a < c).