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 elements
  • ArrayList, if you need to frequently add, delete, query, and modify elements
  • HashSet, if you want each element to be unique
  • HashMap, if you want to store a series of key-value mappings

The following table compares the basic features of these types.

Type nameMutable elementsAdding/Deleting ElementsElement uniquenessOrdered sequence
Array<T>YNNY
ArrayList<T>YYNY
HashSet<T>NYYN
HashMap<K, V>K: N, V: YYK: Y, V: NN