std.collection Package
Function Description
The collection package provides efficient implementation of common data structures, definitions of related abstract interfaces, and common functions in collection types.
This package implements the following common data structures:
-
ArrayList: variable-length continuous array used when an uncertain amount of data needs to be stored or when the array size needs to be dynamically adjusted based on running conditions. Using ArrayList may increase the overhead of memory allocation and release. Therefore, exercise caution when using ArrayList.
-
LinkedList: linked list structure. The advantage of LinkedList is that it can dynamically add or delete elements without moving other elements. This makes it very useful when elements need to be frequently added or deleted. LinkedList can also easily modify or delete elements and store multiple elements in a list. The disadvantage of LinkedList is that it requires extra memory to store references to each element, which may cause memory waste.
-
HashMap: hash table that stores key-value pairs and allows quick access to values based on keys. HashMap is used when mapping relationships need to be used and quick search is required.
-
HashSet: collection data structure implemented based on a hash table. It can be used to quickly search for and delete elements and supports efficient insertion, deletion, and search operations.
-
TreeMap: ordered mapping table implemented based on the red-black tree. Generally, TreeMap is used when elements need to be sorted in natural or user-defined order.
The collection types provided by the collection package do not support concurrency security. For details about the collections that support concurrency security, see collection.concurrent package.
API List
Function
Name | Description |
---|---|
all<T>((T) -> Bool) | Checks whether all elements of an iterator meet the conditions. |
any<T>((T) -> Bool) | Checks whether an element that meets the conditions exists in an iterator. |
at<T>(Int64) | Obtains the element at a specified location of an iterator. |
collectArrayList<T>(Iterable<T>) | Converts an iterator to an instance of the ArrayList type. |
collectArray<T>(Iterable<T>) | Converts an iterator to an instance of the Array type. |
collectHashMap<K, V>(Iterable<(K, V)>) where K <: Hashable & Equatable<K> | Converts an iterator to an instance of the HashMap type. |
collectHashSet<T>(Iterable<T>) where T <: Hashable & Equatable<T> | Converts an iterator to an instance of the HashSet type. |
collectString<T>(String) where T <: ToString | Converts an iterator whose elements implement the ToString API to an instance of the String type. |
concat<T>(Iterable<T>) | Concatenates two iterators in series. |
contains<T>(T) where T <: Equatable<T> | Traverses all elements, checks whether a specified element is included, and returns the element. |
count<T>(Iterable<T>) | Counts the number of elements included in an iterator. |
enumerate<T>(Iterable<T>) | Obtains an iterator with indexes. |
filter<T>((T) -> Bool) | Filters out elements that meet the conditions. |
filterMap<T, R>((T) -> ?R) | Creates an iterator that both filters and maps |
first<T>(Iterable<T>) | Obtains the first element. |
flatMap<T, R>( (T) -> Iterable<R>) | Creates a mapping with the flatten function. |
flatten<T, R>(Iterable<T>) where T <: Iterable<R> | Unfolds one layer of a nested iterator. |
fold<T, R>(R, (R, T) -> R) | Uses a specified initial value to calculate from left to right. |
forEach<T>((T) -> Unit) | Traverses all elements and specifies a specified operation. |
inspect<T>((T) -> Unit) | Performs an extra operation on the current element each time the iterator calls next() (without consuming elements in the iterator). |
isEmpty<T>(Iterable<T>) | Checks whether an iterator is empty. |
last<T>(Iterable<T>) | Obtains the last element. |
map<T, R>((T) -> R) | Creates a mapping. |
max<T>(Iterable<T>) where T <: Comparable<T> | Filters out a maximum element. |
min<T>(Iterable<T>) where T <: Comparable<T> | Filters out a minimum element. |
none<T>((T) -> Bool) | Checks whether an iterator does not meet any conditions. |
reduce<T>((T, T) -> T) | Uses the first element as the initial value to calculate from left to right. |
skip<T>(Int64) | Skips a specific number of elements in an iterator. |
step<T>(Int64) | Each time next() is called, a specific number of elements in the iterator are skipped. |
take<T>(Int64) | Obtains a specific number of elements from an iterator. |
zip<T, R>(Iterable<R>) | Combines two iterators into one (the length depends on the shorter iterator). |
Interface
Name | Description |
---|---|
Map<K, V> where K <: Equatable<K> | Provides a way to map keys to values. |
Set<T> where T <: Equatable<T> | Specifies a collection without duplicate elements. |
EquatableCollection<T> where T <: Equatable<T> | Defines the types of collections that support the comparison operation. |
Class
Name | Description |
---|---|
ArrayList<T> | Provides variable-length arrays. |
ArrayListIterator<T> | Implements the iterator function of ArrayList<T>. |
HashMapIterator<K, V> where K <: Hashable & Equatable<K> | Implements the iterator function of HashMap. |
HashMap<K, V> where K <: Hashable & Equatable<K> | Specifies the hash table implementation of the Map<K, V> where K <: Equatable<K> interface. |
HashSet<T> where T <: Hashable & Equatable<T> | Specifies the class of the Set<T> where T <: Equatable<T> interface that is implemented based on HashMap<K, V> where K <: Hashable & Equatable<K>. |
LinkedListNode<T> | Specifies a node on LinkedList<T>. |
LinkedList<T> | Specifies the data structure with doubly linked list mode implemented. |
TreeMap<K, V> where K <: Comparable<K> | Specifies the class of the Map<K, V> where K <: Equatable<K> interface implemented based on the balanced binary search tree. |
Struct
Name | Description |
---|---|
EntryView<K, V> where K <: Hashable & Equatable<K> | Specifies the view of a key in HashMap<K, V> where K <: Hashable & Equatable<K>. |
TreeMapNode<K, V> where K <: Comparable<K> | Specifies the node structure of TreeMap<K, V> where K <: Comparable<K>. |
Exception Class
Name | Description |
---|---|
ConcurrentModificationException | Specifies an exception thrown upon concurrent modification. |