AnyLogic
Expand
Font size

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

Method Summary

Modifier and TypeMethodDescription
static <T> booleanaddAll(Collection<? super T> c, Collection<T> elements)
Adds all of the elements in the specified collection to this collection.
static <T> booleanaddAll(Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection.
static <T> doubleaverage(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns average value in given collection.
Usage examples:
static <T> doubleaverageWhere(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:
static <T> intcount(Iterable<T> collection, Predicate<? super T> condition)
Returns the number of elements/agents in the given collection which meet the given condition.
Usage examples:
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:
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:
static <T> List<T>findAll(Iterable<T> collection, Predicate<? super T> condition)
This function is the same as filter(Iterable, Predicate)
static <T> List<T>findAll(T[] array, Predicate<? super T> condition)
This function is the same as filter(Object[], Predicate)
static <T> TfindFirst(Iterable<T> collection, Predicate<? super T> condition)
Returns the first element/agent from the given collection which meets the given condition.
Usage example:
static <T> TfindFirst(T[] array, Predicate<? super T> condition)
Returns the first element/agent from the given array which meets the given condition.
Usage example:
static <T> intindexOfFirst(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:
static <T> intindexOfFirst(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:
static <T> doublemax(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns maximum value in given collection.
Usage examples:
static <T> doublemaxWhere(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:
static <T> doublemin(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns minimum value in given collection.
Usage examples:
static <T> doubleminWhere(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:
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.
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.
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.
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.
static <T> doublesum(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns sum of values in given collection.
Usage examples:
static <T> doublesumWhere(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:
static <T> Ttop(Iterable<T> collection, ToDoubleFunction<? super T> value)
Returns element having maximum value in given collection.
Usage examples:

Methods inherited from class com.anylogic.engine.UtilitiesStream

streamOfElements, streamOfElements

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Details

filter

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:
 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 through
condition - the condition to test
Returns:
always new instance of list, modifiable, with elements which have true value of the given condition

filter

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:
 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 through
condition - the condition to test
Returns:
always new instance of list, modifiable, with elements which have true value of the given condition

findAll

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:

findAll

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:

findFirst

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:
 Person person = findFirst( people, p -> p.age > 20 );
 
Parameters:
collection - the collection of elements/agents to be iterated through
condition - the condition to test
Returns:
the element which has true value of the given condition, or null if there is no such element or the collection is empty.
Since:
7.2

findFirst

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:
 Person person = findFirst( peopleArray, p -> p.age > 20 );
 
Parameters:
array - the array of elements/agents to be iterated through
condition - the condition to test
Returns:
the element which has true value of the given condition, or null if there is no such element or the array is empty.
Since:
7.2

indexOfFirst

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:
 int i = indexOfFirst( people, p -> p.age > 20 );
 
Parameters:
collection - the collection of elements/agents to be iterated through
condition - 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

indexOfFirst

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:
 int i = indexOfFirst( peopleArray, p -> p.age > 20 );
 
Parameters:
array - the array of elements/agents to be iterated through
condition - 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

count

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:
 traceln(count( people, p -> p.income > 10000 ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
condition - the condition to test
Returns:
the number of agents in the given collection which have true value of the given condition

max

public static <T> double max(Iterable<T> collection,
 ToDoubleFunction<? super T> value)
Returns maximum value in given collection.
Usage examples:
 traceln(max( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
maximum value in given collection.
Returns -infinity if collection is empty

maxWhere

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:
 traceln(maxWhere( people, p -> p.income, p -> p.age > 18 ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
maximum value in given collection with given condition.
Returns -infinity if collection is empty

min

public static <T> double min(Iterable<T> collection,
 ToDoubleFunction<? super T> value)
Returns minimum value in given collection.
Usage examples:
 traceln(min( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
minimum value in given collection.
Returns +infinity if collection is empty

minWhere

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:
 traceln(minWhere( people, p -> p.income, p -> p.age > 18 ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
minimum value in given collection with given condition.
Returns +infinity if collection is empty

sum

public static <T> double sum(Iterable<T> collection,
 ToDoubleFunction<? super T> value)
Returns sum of values in given collection.
Usage examples:
 traceln(sum( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
sum of values in given collection

sumWhere

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:
 traceln(sumWhere( people, p -> p.income, p -> p.age > 18 ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
sum of values in given collection with given condition

average

public static <T> double average(Iterable<T> collection,
 ToDoubleFunction<? super T> value)
Returns average value in given collection.
Usage examples:
 traceln(average( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
average of elements value in given collection, or NaN if the collection is empty

averageWhere

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:
 traceln(averageWhere( people, p -> p.income, p -> p.age > 18 ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
average of values in given collection with given condition, or NaN if the collection has no matching elements

top

public static <T> T top(Iterable<T> collection,
 ToDoubleFunction<? super T> value)
Returns element having maximum value in given collection.
Usage examples:
 traceln(top( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
element having maximum value in given collection.
Returns null if collection is empty

sortAscending

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)

sortDescending

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)

sortAscending

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.
 sortAscending( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
list sorted ascending by value.

sortDescending

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.
 sortDescending( people, p -> p.income ));
 
Parameters:
collection - the collection of elements/agents to be iterated through during calculation
value - value transforming T into double
Returns:
list sorted descending by value.

addAll

@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 inserted
elements - the elements to insert into c
Returns:
true if the collection changed as a result of the call
Since:
7.2
See Also:

addAll

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)