| Function | Description | Example |
|---|---|---|
| List filter(collection, condition) | Returns a subset of a given collection — a new list with elements from the original collection which meet the given condition. |
List women = filter( people, p -> p.gender == FEMALE ); List idleTrucks = filter( trucks, t -> t.inState(Idle) ); |
| List findAll(collection, condition) | This function is identical to filter(collection, condition). | - |
| findFirst(collection, condition) | Returns the first element from the given collection which meets the given condition (the element which has true value of the given condition). Returns null if there is no such element or the collection is empty. | Person person = findFirst( people, p -> p.age > 20 ); |
| T findMax(collection) |
Returns the “maximum” element in the given collection according to natural ordering (elements should be comparable). If there are multiple elements which are the “maximum”, the first one is returned. |
double biggestSalary = findMax(salaries); |
| T findMax(collection, condition) |
Returns the “maximum” element in the given collection according to the specified condition. If there are multiple elements with the same maximum value, the first one is returned. |
Person highestEarner = findMax( people, p -> p.income ); |
| T findMin(collection) |
Returns the “minimum” element in the given collection according to natural ordering (elements should be comparable). If there are multiple elements which are the “minimum”, the first one is returned. |
double lowestSalary = findMin(salaries); |
| T findMin(collection, condition) |
Returns the “minimum” element in the given collection according to the specified condition. If there are multiple elements with the same minimum value, the first one is returned. |
Person lowestEarner = findMin( people, p -> p.income ); |
AnyLogic provides functions to select a random element from a given collection. These functions are defined in the class Utilities and are global (accessible from each place of model code). You call any function from the list below by typing just its name, without a prefix with the collection name: randomWhere( people, p -> p.income > 10000 );
-
randomWhere(collection, condition) — Returns one randomly selected element within the given collection satisfying the specified condition. If the collection is empty, or none elements satisfy the specified condition(s), the function returns null.
Examples:
Person wealthyPerson = randomWhere( people, p -> p.income > 10000 );
Person wealthyMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE );
Here we check two conditions (we use logical AND operator && to tell AnyLogic that both the first AND the second condition should be true). The function returns a randomly selected element that satisfies both conditions.
Person wealthyYoungMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE && p.age < 21 );
There is another function that uses a custom random number generator to select the element instead of the default one:
randomWhere( collection, condition, java.util.Random r )
Other than that, this function is similar to randomWhere(population, condition).
-
How can we improve this article?
-