- java.lang.Object
- com.anylogic.engine.UtilitiesStream
- com.anylogic.engine.UtilitiesCollection
public final class UtilitiesCollection extends UtilitiesStream
This class provides a lot of commonly used functions for operations with collections / agent populations
- Author:
- AnyLogic North America, LLC https://anylogic.com
Modifier and Type | Method | Description |
---|---|---|
static <T> boolean | addAll |
Adds all of the elements in the specified collection to this collection.
|
static <T> boolean | addAll |
Adds all of the specified elements to the specified collection.
|
static <T> double | average |
Returns average value in given collection.
Usage examples: |
static <T> double | averageWhere |
Returns average of values in given collection
among elements which meet the given condition. Usage examples: |
static <T> int | count |
Returns the number of elements/agents in the given
collection which meet the given condition.
Usage examples: |
static <T> List<T> | filter |
Returns new list with elements/agents from original collection
which meet the given condition.
Usage examples: |
static <T> List<T> | filter |
Returns new list with elements/agents from original array
which meet the given condition.
Usage examples: |
static <T> List<T> | findAll |
This function is the same as
filter(Iterable, Predicate) |
static <T> List<T> | findAll |
This function is the same as
filter(Object[], Predicate) |
static <T> T | findFirst |
Returns the first element/agent from the given collection
which meets the given condition.
Usage example: |
static <T> T | findFirst |
Returns the first element/agent from the given array
which meets the given condition.
Usage example: |
static <T> int | indexOfFirst |
Returns the index of the first element/agent from the given collection
which meets the given condition.
Usage example: |
static <T> int | indexOfFirst |
Returns the index of the first element/agent from the given array
which meets the given condition.
Usage example: |
static <T> double | max |
Returns maximum value in given collection.
Usage examples: |
static <T> double | maxWhere |
Returns maximum value of element in given collection
among elements which meet the given condition. Usage examples: |
static <T> double | min |
Returns minimum value in given collection.
Usage examples: |
static <T> double | minWhere |
Returns minimum value of element in given collection
among elements which meet the given condition. Usage examples: |
static <T> List<T> | sortAscending |
Returns new list with rearranged elements/agents
from given collection sorted ascending by value. |
static <T extends Comparable<? super T>> | sortAscending |
Sorts the specified list into ascending order, according to the
natural ordering of its elements.
|
static <T> List<T> | sortDescending |
Returns new list with rearranged elements/agents
from given collection sorted descending by value. |
static <T extends Comparable<? super T>> | sortDescending |
Sorts the specified list into ascending order, according to the
inverted natural ordering of its elements.
|
static <T> double | sum |
Returns sum of values in given collection.
Usage examples: |
static <T> double | sumWhere |
Returns sum of values in given collection
among elements which meet the given condition. Usage examples: |
static <T> T | top |
Returns element having maximum value in given collection.
Usage examples: |
public static <T> List<T> filter(Iterable<T> collection, Predicate<? super T> condition)
Returns new list with elements/agents from original collection
which meet the given condition.
Usage examples:
Usage examples:
for (Person person : filter( people, p -> p.age > 20 )) { traceln( person ); } Person person = randomFrom( filter( people, p -> p.income < 5000 ) );
- Parameters:
collection
- the collection of elements/agents to be iterated throughcondition
- the condition to test- Returns:
- always new instance of list, modifiable,
with elements which have
true
value of the given condition
public static <T> List<T> filter(T[] array, Predicate<? super T> condition)
Returns new list with elements/agents from original array
which meet the given condition.
Usage examples:
Usage examples:
for (Person person : filter( peopleArray, p -> p.age > 20 )) { traceln( person ); } Person person = randomFrom( filter( peopleArray, p -> p.income < 5000 ) );
- Parameters:
array
- the array of elements/agents to be iterated throughcondition
- the condition to test- Returns:
- always new instance of list, modifiable,
with elements which have
true
value of the given condition
public static <T> List<T> findAll(Iterable<T> collection, Predicate<? super T> condition)
This function is the same as
filter(Iterable, Predicate)
- Since:
- 7.2
- See Also:
public static <T> List<T> findAll(T[] array, Predicate<? super T> condition)
This function is the same as
filter(Object[], Predicate)
- Since:
- 7.2
- See Also:
public static <T> T findFirst(Iterable<T> collection, Predicate<? super T> condition)
Returns the first element/agent from the given collection
which meets the given condition.
Usage example:
Usage example:
Person person = findFirst( people, p -> p.age > 20 );
- Parameters:
collection
- the collection of elements/agents to be iterated throughcondition
- the condition to test- Returns:
- the element which has
true
value of the given condition, ornull
if there is no such element or the collection is empty. - Since:
- 7.2
public static <T> T findFirst(T[] array, Predicate<? super T> condition)
Returns the first element/agent from the given array
which meets the given condition.
Usage example:
Usage example:
Person person = findFirst( peopleArray, p -> p.age > 20 );
- Parameters:
array
- the array of elements/agents to be iterated throughcondition
- the condition to test- Returns:
- the element which has
true
value of the given condition, ornull
if there is no such element or the array is empty. - Since:
- 7.2
public static <T> int indexOfFirst(Iterable<T> collection, Predicate<? super T> condition)
Returns the index of the first element/agent from the given collection
which meets the given condition.
Usage example:
Usage example:
int i = indexOfFirst( people, p -> p.age > 20 );
- Parameters:
collection
- the collection of elements/agents to be iterated throughcondition
- the condition to test- Returns:
- the index of the first element which has
true
value of the given condition, or-1
if there is no such element or the collection is empty. - Since:
- 8.5.0
public static <T> int indexOfFirst(T[] array, Predicate<? super T> condition)
Returns the index of the first element/agent from the given array
which meets the given condition.
Usage example:
Usage example:
int i = indexOfFirst( peopleArray, p -> p.age > 20 );
- Parameters:
array
- the array of elements/agents to be iterated throughcondition
- the condition to test- Returns:
- the index of the first element which has
true
value of the given condition, or-1
if there is no such element or the array is empty. - Since:
- 8.5.0
public static <T> int count(Iterable<T> collection, Predicate<? super T> condition)
Returns the number of elements/agents in the given
collection which meet the given condition.
Usage examples:
Usage examples:
traceln(count( people, p -> p.income > 10000 ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationcondition
- the condition to test- Returns:
- the number of agents in the given
collection which have
true
value of the given condition
public static <T> double max(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns maximum value in given collection.
Usage examples:
Usage examples:
traceln(max( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- maximum value in given collection.
Returns -infinity if collection is empty
public static <T> double maxWhere(Iterable<T> collection, ToDoubleFunction<? super T> value, Predicate<? super T> condition)
Returns maximum value of element in given collection
among elements which meet the given condition.
Usage examples:
among elements which meet the given condition.
Usage examples:
traceln(maxWhere( people, p -> p.income, p -> p.age > 18 ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- maximum value in given collection with given condition.
Returns -infinity if collection is empty
public static <T> double min(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns minimum value in given collection.
Usage examples:
Usage examples:
traceln(min( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- minimum value in given collection.
Returns +infinity if collection is empty
public static <T> double minWhere(Iterable<T> collection, ToDoubleFunction<? super T> value, Predicate<? super T> condition)
Returns minimum value of element in given collection
among elements which meet the given condition.
Usage examples:
among elements which meet the given condition.
Usage examples:
traceln(minWhere( people, p -> p.income, p -> p.age > 18 ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- minimum value in given collection with given condition.
Returns +infinity if collection is empty
public static <T> double sum(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns sum of values in given collection.
Usage examples:
Usage examples:
traceln(sum( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- sum of values in given collection
public static <T> double sumWhere(Iterable<T> collection, ToDoubleFunction<? super T> value, Predicate<? super T> condition)
Returns sum of values in given collection
among elements which meet the given condition.
Usage examples:
among elements which meet the given condition.
Usage examples:
traceln(sumWhere( people, p -> p.income, p -> p.age > 18 ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- sum of values in given collection with given condition
public static <T> double average(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns average value in given collection.
Usage examples:
Usage examples:
traceln(average( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- average of elements value in given collection, or
NaN
if the collection is empty
public static <T> double averageWhere(Iterable<T> collection, ToDoubleFunction<? super T> value, Predicate<? super T> condition)
Returns average of values in given collection
among elements which meet the given condition.
Usage examples:
among elements which meet the given condition.
Usage examples:
traceln(averageWhere( people, p -> p.income, p -> p.age > 18 ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- average of values in given collection with given condition, or
NaN
if the collection has no matching elements
public static <T> T top(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns element having maximum value in given collection.
Usage examples:
Usage examples:
traceln(top( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- element having maximum value in given collection.
Returns null if collection is empty
public static <T extends Comparable<? super T>> void sortAscending(List<T> list)
Sorts the specified list into ascending order, according to the
natural ordering of its elements.
All elements in the list must implement the
Comparable
interface.
The specified list must be modifiable
- Type Parameters:
T
- the class of the objects in the list- Parameters:
list
- the list to be sorted.- Since:
- 8.0
- See Also:
-
List.sort(Comparator)
public static <T extends Comparable<? super T>> void sortDescending(List<T> list)
Sorts the specified list into ascending order, according to the
inverted natural ordering of its elements.
All elements in the list must implement the
Comparable
interface.
The specified list must be modifiable
- Type Parameters:
T
- the class of the objects in the list- Parameters:
list
- the list to be sorted.- Since:
- 8.0
- See Also:
-
List.sort(Comparator)
public static <T> List<T> sortAscending(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns new list with rearranged elements/agents
from given collection sorted ascending by value.
from given collection sorted ascending by value.
sortAscending( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- list sorted ascending by value.
public static <T> List<T> sortDescending(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns new list with rearranged elements/agents
from given collection sorted descending by value.
from given collection sorted descending by value.
sortDescending( people, p -> p.income ));
- Parameters:
collection
- the collection of elements/agents to be iterated through during calculationvalue
- value transforming T into double- Returns:
- list sorted descending by value.
@SafeVarargs public static <T> boolean addAll(Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection.
Elements to be added may be specified individually or as an array.
The behavior of this convenience method is identical to that of
c.addAll(Arrays.asList(elements)), but this method is likely
to run significantly faster under most implementations.
When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:
Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
- Parameters:
c
- the collection into which elements are to be insertedelements
- the elements to insert into c- Returns:
- true if the collection changed as a result of the call
- Since:
- 7.2
- See Also:
public static <T> boolean addAll(Collection<? super T> c, Collection<T> elements)
Adds all of the elements in the specified collection to this collection.
- Parameters:
c
- collection containing elements to be added to this collection- Returns:
- true if this collection changed as a result of the call
- Since:
- 7.2
- See Also:
-
Collection.addAll(Collection)