AnyLogic AnyLogic
Expand
Font size
  • java.lang.Object
    • com.anylogic.engine.UtilitiesCollection

public final class UtilitiesCollection
extends java.lang.Object
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

All Methods Static Methods Concrete Methods 
static <T> boolean addAll​(java.util.Collection<? super T> c, java.util.Collection<T> elements)
Adds all of the elements in the specified collection to this collection.
static <T> boolean addAll​(java.util.Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection.
static <T> double average​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns average value in given collection.
Usage examples:
static <T> double averageWhere​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value, java.util.function.Predicate<? super T> condition)
Returns average of values in given collection
among elements which meet the given condition.
Usage examples:
static <T> int count​(java.lang.Iterable<T> collection, java.util.function.Predicate<? super T> condition)
Returns the number of elements/agents in the given collection which meet the given condition.
Usage examples:
static <T> java.util.List<T> filter​(java.lang.Iterable<T> collection, java.util.function.Predicate<? super T> condition)
Returns new list with elements/agents from original collection which meet the given condition.
Usage examples:
static <T> java.util.List<T> filter​(T[] array, java.util.function.Predicate<? super T> condition)
Returns new list with elements/agents from original array which meet the given condition.
Usage examples:
static <T> java.util.List<T> findAll​(java.lang.Iterable<T> collection, java.util.function.Predicate<? super T> condition)
This function is the same as filter(Iterable, Predicate)
static <T> java.util.List<T> findAll​(T[] array, java.util.function.Predicate<? super T> condition)
This function is the same as filter(Object[], Predicate)
static <T> T findFirst​(java.lang.Iterable<T> collection, java.util.function.Predicate<? super T> condition)
Returns the first element/agent from the given collection which meets the given condition.
Usage example:
static <T> T findFirst​(T[] array, java.util.function.Predicate<? super T> condition)
Returns the first element/agent from the given array which meets the given condition.
Usage example:
static <T> int indexOfFirst​(java.lang.Iterable<T> collection, java.util.function.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> int indexOfFirst​(T[] array, java.util.function.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> double max​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns maximum value in given collection.
Usage examples:
static <T> double maxWhere​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value, java.util.function.Predicate<? super T> condition)
Returns maximum value of element in given collection
among elements which meet the given condition.
Usage examples:
static <T> double min​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns minimum value in given collection.
Usage examples:
static <T> double minWhere​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value, java.util.function.Predicate<? super T> condition)
Returns minimum value of element in given collection
among elements which meet the given condition.
Usage examples:
static <T> java.util.List<T> sortAscending​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns new list with rearranged elements/agents
from given collection sorted ascending by value.
static <T extends java.lang.Comparable<? super T>>
void
sortAscending​(java.util.List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.
static <T> java.util.List<T> sortDescending​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns new list with rearranged elements/agents
from given collection sorted descending by value.
static <T extends java.lang.Comparable<? super T>>
void
sortDescending​(java.util.List<T> list)
Sorts the specified list into ascending order, according to the inverted natural ordering of its elements.
static <T> java.util.stream.Stream<T> streamOfElements​(java.util.Collection<? extends T>... lists)  
static <T> java.util.stream.Stream<T> streamOfElements​(java.util.stream.Stream<? extends T>... streams)  
static <T> double sum​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns sum of values in given collection.
Usage examples:
static <T> double sumWhere​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value, java.util.function.Predicate<? super T> condition)
Returns sum of values in given collection
among elements which meet the given condition.
Usage examples:
static <T> T top​(java.lang.Iterable<T> collection, java.util.function.ToDoubleFunction<? super T> value)
Returns element having maximum value in given collection.
Usage examples:
Modifier and Type Method Description

Methods inherited from class java.lang.Object

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

Method Detail

filter

public static <T> java.util.List<T> filter​(java.lang.Iterable<T> collection,
                                           java.util.function.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> java.util.List<T> filter​(T[] array,
                                           java.util.function.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> java.util.List<T> findAll​(java.lang.Iterable<T> collection,
                                            java.util.function.Predicate<? super T> condition)
This function is the same as filter(Iterable, Predicate)
Since:
7.2
See Also:
filter(Iterable, Predicate)

findAll

public static <T> java.util.List<T> findAll​(T[] array,
                                            java.util.function.Predicate<? super T> condition)
This function is the same as filter(Object[], Predicate)
Since:
7.2
See Also:
filter(Object[], Predicate)

findFirst

public static <T> T findFirst​(java.lang.Iterable<T> collection,
                              java.util.function.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,
                              java.util.function.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​(java.lang.Iterable<T> collection,
                                   java.util.function.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,
                                   java.util.function.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​(java.lang.Iterable<T> collection,
                            java.util.function.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​(java.lang.Iterable<T> collection,
                             java.util.function.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​(java.lang.Iterable<T> collection,
                                  java.util.function.ToDoubleFunction<? super T> value,
                                  java.util.function.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​(java.lang.Iterable<T> collection,
                             java.util.function.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​(java.lang.Iterable<T> collection,
                                  java.util.function.ToDoubleFunction<? super T> value,
                                  java.util.function.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​(java.lang.Iterable<T> collection,
                             java.util.function.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​(java.lang.Iterable<T> collection,
                                  java.util.function.ToDoubleFunction<? super T> value,
                                  java.util.function.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​(java.lang.Iterable<T> collection,
                                 java.util.function.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​(java.lang.Iterable<T> collection,
                                      java.util.function.ToDoubleFunction<? super T> value,
                                      java.util.function.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​(java.lang.Iterable<T> collection,
                        java.util.function.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 java.lang.Comparable<? super T>> void sortAscending​(java.util.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 java.lang.Comparable<? super T>> void sortDescending​(java.util.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> java.util.List<T> sortAscending​(java.lang.Iterable<T> collection,
                                                  java.util.function.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> java.util.List<T> sortDescending​(java.lang.Iterable<T> collection,
                                                   java.util.function.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​(java.util.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:
Collections.addAll(Collection, Object...)

addAll

public static <T> boolean addAll​(java.util.Collection<? super T> c,
                                 java.util.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)

streamOfElements

@AnyLogicInternalAPI
@SafeVarargs
public static <T> java.util.stream.Stream<T> streamOfElements​(java.util.Collection<? extends T>... lists)

streamOfElements

@AnyLogicInternalAPI
@SafeVarargs
public static <T> java.util.stream.Stream<T> streamOfElements​(java.util.stream.Stream<? extends T>... streams)
How can we improve this article?