Choose where you’d like to start

Collection data type

Note: Collection data type is not to be confused with a collection variable. To learn about a collection variable, which is used to hold a collection of records in Zoho Creator, refer to this page.

Overview

Collection allows you to store, insert, retrieve, delete, and manipulate a group of data.

The values in a collection can be of any basic data type. Or, the values themselves could be collections.

Types

The following two types are the broad classification of collection.

  • Index-value collection

    The index-value collection holds the data in an ordered manner, i.e., it retains the order in which the values are inserted. The order is marked by a value positioning entity, index.

    The indexes are numbers that identify values in a collection. The index of the first value in a collection is always 0 and the subsequent values are 1, 2, 3, and so forth. Note that for a collection that has "n" number of elements, the indexes range from 0 to (n-1).

    Example

    The following collection has 3 values. Therefore, the index starts from 0 and extends up to 2.

    Index

    0

    1

    2

    Value

    Shawn

    Alan

    Kate

    The index of Shawn, Alan, and Kate are 0, 1, and 2 respectively.

  • Key-value collection

    The key-value collection does not hold values in an ordered manner. Instead, the values are identified by their associated keys. The keys must be unique. If a new value is inserted with an existing key, it will overwrite the existing value.

    Note: Both keys and values can be of any data type.

    Example

    The following collection contains three values, each identified by its respective key.

    Key

    Name

    Gender

    Company

    Value

    Shawn

    Male

    Zoho

    The values - Shawn, Male, and Zoho are identified by the keys - Name, Gender, and Company respectively. Here, the values are arranged in no particular order, but each value is associated with its respective key.

Note: A collection can contain any number of elements or key-value pairs.

Create a collection

The following is the syntax to create an empty collection.

 <collection_variable> = Collection();
 

The following is the syntax to create a collection and initialize it with values.

 <collection_variable> = Collection(<values>);
 

Example 1: Create an index-value collection

The following example creates an index-value collection with the values - Mobile phone and Laptop.

 gadgets = Collection("Mobile phone","Laptop");
 

Here, the values - Mobile phone and Laptop are identified by the indexes - 0 and 1 respectively.

Example 2: Create a key-value collection

The following example creates a key-value collection with the key-value pairs - "Name":"Shawn" and "Gender":"Male".

 details = Collection("Name":"Shawn","Gender":"Male");
 

Here, the values - Shawn and Male are identified by the keys - Name and Gender respectively.

Insert data into a collection

The built-in function - insert can be used to add one or more values into an existing collection. The following is the syntax to insert new values into a collection.

 <collection_variable>.insert(<new_value>);
 

Example 1: Insert values into an index-value collection

The following example inserts the values - "Hard disk" and "Camera" into the collection - gadgets.

 gadgets.insert("Hard disk" , "Camera");
 

Example 2: Insert values into a key-value collection

The following example inserts the key-value pair - "Company":"Zoho" into the collection - details.

 details.insert("Company":"Zoho");
 

Retrieve data from a collection

The built-in function - get can be used to fetch the required value from a collection. The following is the syntax to get the required value from a collection.

 <value> = <collection_variable>.get(<index/key>);
 

Example 1: Fetch values from a index-value collection

The following example fetches the first value from the collection - gadgets.

 value = gadgets.get(0);
 

Example 2: Fetch values from a key-value collection

The following example fetches the value associated with the key - "Name" from the collection - details.

 value = details.get("Name");
 
Note: Click here for other built-in functions to manipulate the data stored in a collection.

Iterate through a collection

The for each task allows you to traverse the entire collection and inspect each value one after the other. This functionality lets you perform similar operations to a group of values with a relatively lesser lines of code.

The following example uses the for each task to find the sum of all the values in a collection of numbers.

 // initializes the variable - sum with the value - 0
 sum = 0;
 
 // Creates an index-value collection initialized with NUMBER values
 collection_variable = Collection(1,2,3);
 
 // Iterates through the collection created
 for each value in collection_variable
 {
 // Performs addition operation
 sum = sum + value;
 }
 
 info sum; // Displays the sum value, 6
 

where,

sum
The NUMBER variable that holds the aggregated total of all the values in the collection.
collection_variable
The INDEX-VALUE variable that holds the list of values whose sum needs to be calculated.
value
The iteration variable. It holds every value in the collection, one at a time.
In other words, the iteration variable - value holds the first value on the first iteration, the second value on the second iteration, and so forth.
The number of iterations is the number of values present in the collection.

List/Key-Value vs Collection

When a variable is declared as a LIST data type or a Key-Value data type, it can only hold values that are applicable to that particular data type. For example, a list variable can only hold elements, and a Key-Value variable can only hold key-value pairs.

However, both list and Key-Value data types are fundamentally a collection of values, in the sense that, a list data type holds a collection of elements, and a Key-Value data type holds a collection of key-value pairs. Consequently, there are built-in functions that are common to both the data types, but are maintained as two different sets of functions. For example, the put() map function is used to insert a key-value pair in a Key-Value variable, and the add() list function is used to insert an element in a list variable.

To make things easier, we have come up with the "collection" data type, where one can simply declare a collection variable, and use it to store either elements or key-value pairs. Note that each collection variable at a time can act only as a list variable containing elements, or as a Key-Value variable containing key-value pairs. It cannot contain elements and key-value pairs together in a single collection. We also have a set of built-in functions relevant to the collection data type, which can be applied to the collection variable irrespective of it holding list values or Key-Value values. For example, the insert() collection function can be used to insert a key-value pair or an element, depending on the collection variable holding key-value pairs or elements.

 
Note:
  • See this page to find the built-in functions applicable to Collection data type.

Related Links

Get Started Now

Execute