Basic Collection Type Overview
In the most general sense, a collection is an abstract data type representing a grouping of elements of the same type. Each particular collection type provides a specific set of operations and possesses unique performance and memory footprint characteristics.
We have already discussed one widely used collection type, Array
, which is a fixed-size linearly indexed sequence of elements (see Array for details). In this chapter, we'll take a closer look at a few more basic collection types commonly used in Cangjie programs: ArrayList
, HashSet
, and HashMap
.
Among those four types, you would normally select the most suitable for the problem at hand:
Array
, if you do not need to add or delete elements, but possibly need to modify elementsArrayList
, if you need to frequently add, delete, query, and modify elementsHashSet
, if you want each element to be uniqueHashMap
, if you want to store a series of key-value mappings
The following table compares the basic features of these types.
Type name | Mutable elements | Adding/Deleting Elements | Element uniqueness | Ordered sequence |
---|---|---|---|---|
Array<T> | Y | N | N | Y |
ArrayList<T> | Y | Y | N | Y |
HashSet<T> | N | Y | Y | N |
HashMap<K, V> | K: N, V: Y | Y | K: Y, V: N | N |