std.collection
Overview
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:
-
ArrayDeque: double-ended circular queue implemented based on arrays. It supports addition and deletion of the first and last elements of a collection. The addFirst() and addLast() methods can be used to add the first and last elements of a collection, and the removeFirst() and removeLast() methods can be used to delete the first and last elements from a collection.
-
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.
-
ArrayQueue: circular queue implemented based on arrays. It only allows the last element to be added and the first element to be deleted.
-
ArrayStack: stack data structure implemented based on arrays. It follows the first in last out rule and supports data addition and deletion only at the top.
-
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.
-
TreeSet: ordered collection implemented based on TreeMap. It can be used to automatically sort elements and store unique data that needs to be sorted.
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) | Skips a specific number of elements each time an iterator calls next(). |
| 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 |
|---|---|
| Deque<T> | Specifies a double-ended queue which is a data structure with queue and stack features, allowing addition and deletion operations at both ends. |
| EquatableCollection<T> | Defines the types of collections that support the comparison operation. |
| List<T> | Provides index-friendly list operations. |
| Map<K, V> | Provides a method of mapping keys to values. |
| MapEntryView<K, V> | Specifies a view of a key in a key-value pair collection. |
| OrderedMap<K, V> | Specifies an ordered mapping. |
| OrderedSet<T> | Specifies an ordered collection. |
| Queue<T> | Specifies a queue data structure that complies with the first in first out (FIFO) principle. |
| ReadOnlyList<T> | Defines read-only operations on a list. |
| ReadOnlyMap<K, V> | Specifies a read-only mapping. |
| ReadOnlySet<K, V> | Specifies a read-only collection. |
| Set<T> | Specifies a collection without duplicate elements. |
| Stack<T> | Specifies a stack data structure that follows the last in first out (LIFO) principle. |
Class
| Name | Description |
|---|---|
| ArrayDeque<T> | Specifies a double-ended queue that is implemented based on resizable circular arrays. |
| ArrayList<T> | Provides the functionalities of a variable-length array. |
| ArrayQueue<T> | Specifies a circular queue data structure implemented based on arrays. |
| ArrayStack<T> | Specifies the Stack data structure implemented based on arrays. |
| HashMapIterator<K, V> where K <: Hashable & Equatable<K> | Implements the iterator functionality of HashMap. |
| HashMap<K, V> where K <: Hashable & Equatable<K> | Specifies the hash table implementation of the Map<K, V> interface. |
| HashSet<T> where T <: Hashable & Equatable<T> | Specifies an instance of the Set<T> interface implemented based on HashMap<K, V>. |
| 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 an instance of the Map<K, V> interface implemented based on a balanced binary search tree. |
| TreeSet<T> where T <: Comparable<T> | Specifies an instance of the Set<T> interface implemented based on TreeMap<K, V>. |
Exception Class
| Name | Description |
|---|---|
| ConcurrentModificationException | Specifies an exception thrown upon concurrent modification. |