Collection data type

Collection data type

This guide will help you with the following:
  1. Overview
  2. Types
  3. Create a collection
  4. Insert data into a collection
  5. Retrieve data from a collection
  6. Iterate through a collection
  7. List/Map vs Collection
Note: Collection data type is not to be confused with a collection variable.

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.

1. 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.

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

2. 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.

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.
  1.  <collection_variable>=Collection();
 
The following is the syntax to create a collection and initialize it with values.
  1. <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.
  1. 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".
  1. 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.
  1. <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.
  1. 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.
  1. 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.
  1. <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.
  1. 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.
  1. 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.
  1. // initializes the variable - sum with the value - 0  
  2. sum = 0;    
  3. // Creates an index-value collection initialized with NUMBER values  
  4. collection_variable = Collection(1,2,3);    
  5. // Iterates through the collection created  
  6. for each value in collection_variable  
  7. {  
  8. // Performs addition operation  
  9. sum = sum + value;  
  10. }    
  11. info sum; // Displays the sum value, 6 
where, 


List/Map vs Collection

When a variable is declared as a LIST data type or a MAP 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 map variable can only hold key-value pairs.

However, both list and map data types are fundamentally a collection of values, in the sense that, a list data type holds a collection of elements, and a map 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 map 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 map 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 map 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.

    Zoho CRM Training Programs

    Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

    Zoho CRM Training
      Redefine the way you work
      with Zoho Workplace

        Zoho DataPrep Personalized Demo

        If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

        Zoho CRM Training

          Create, share, and deliver

          beautiful slides from anywhere.

          Get Started Now


            Zoho Sign now offers specialized one-on-one training for both administrators and developers.

            BOOK A SESSION








                                You are currently viewing the help pages of Qntrl’s earlier version. Click here to view our latest version—Qntrl 3.0's help articles.




                                    Manage your brands on social media

                                      Zoho Desk Resources

                                      • Desk Community Learning Series


                                      • Digest


                                      • Functions


                                      • Meetups


                                      • Kbase


                                      • Resources


                                      • Glossary


                                      • Desk Marketplace


                                      • MVP Corner


                                      • Word of the Day


                                        Zoho Marketing Automation

                                          Zoho Sheet Resources

                                           

                                              Zoho Forms Resources


                                                Secure your business
                                                communication with Zoho Mail


                                                Mail on the move with
                                                Zoho Mail mobile application

                                                  Stay on top of your schedule
                                                  at all times


                                                  Carry your calendar with you
                                                  Anytime, anywhere




                                                        Zoho Sign Resources

                                                          Sign, Paperless!

                                                          Sign and send business documents on the go!

                                                          Get Started Now




                                                                  Zoho TeamInbox Resources



                                                                          Zoho DataPrep Resources



                                                                            Zoho DataPrep Demo

                                                                            Get a personalized demo or POC

                                                                            REGISTER NOW


                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now







                                                                                            You are currently viewing the help articles of Sprints 1.0. If you are a user of 2.0, please refer here.

                                                                                            You are currently viewing the help articles of Sprints 2.0. If you are a user of 1.0, please refer here.



                                                                                                  • Related Articles

                                                                                                  • Data Access - Collection variable

                                                                                                    This guide will help you with the following: Overview Declaring a collection variable Aggregate functions Fetch field value from the first record Fetch values of a field from all the records in a collection Updating field value in a collection ...
                                                                                                  • Collection Functions

                                                                                                    This guide will help you with the following: 1. Clear 2. containsKey 3. containsValue 4. delete 5. deleteAll 6. deleteKey 7. deleteKeys 8. distinct 9. duplicate 10. get 11. getAsString 12. getKey 13. getLastKey 14. insert 15. insertAll 16. intersect ...
                                                                                                  • Type conversion functions

                                                                                                    toDate The toDate function takes an expression as an argument, and returns the expression (containing a date-time value) in the date format as specified in application settings.  This function takes an optional argument dateTimeMapping when the given ...
                                                                                                  • Data types

                                                                                                    This guide will help you with the following: Text Number Decimal Boolean Data-time Time List Key-value Text data type The text, or string, datatype represents a sequence of characters. These characters can be text characters, special characters, ...
                                                                                                  • Data Access - Update records

                                                                                                    This guide will help you with the following: Overview Syntax Applicable data-type and expression for each field Things to keep in mind Example Overview The update records deluge task updates(replaces) the value of a specified field with the given ...
                                                                                                    Wherever you are is as good as
                                                                                                    your workplace

                                                                                                      Resources

                                                                                                      Videos

                                                                                                      Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.



                                                                                                      eBooks

                                                                                                      Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.



                                                                                                      Webinars

                                                                                                      Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.



                                                                                                      CRM Tips

                                                                                                      Make the most of Zoho CRM with these useful tips.



                                                                                                        Zoho Show Resources