Collection Data Types in Salesforce: String, Map, List, Date, Set, Number

Collection Data Types in Salesforce

Question

Which of the following are collection data types? -> String -> Map -> List -> Date -> Set -> Number.

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

D.

The collection data types are used to store a group of related values in a single variable. In Apex and Visualforce controllers, there are several collection data types available, including lists, sets, and maps.

  • List: A list is an ordered collection of elements of the same type. The type of elements in a list can be a primitive data type, a user-defined data type, or another collection data type. Lists are declared using square brackets ([]), and the elements in a list are separated by commas. For example, List<String> myList = new List<String>{'apple', 'banana', 'orange'};

  • Set: A set is an unordered collection of unique elements of the same data type. Sets are declared using curly braces ({}) and the elements in a set are separated by commas. For example, Set<String> mySet = new Set<String>{'apple', 'banana', 'orange'};

  • Map: A map is a collection of key-value pairs, where each key maps to a value. Maps are declared using the Map keyword, and the elements in a map are separated by commas. For example, Map<String, String> myMap = new Map<String, String>{'apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange'};

  • String: A string is a sequence of characters. In Apex, strings are represented using double quotes (""). For example, String myString = "Hello World";

  • Number: Number data types include integers, longs, doubles, and decimals. These data types are used to store numeric values. For example, Integer myInt = 10;

  • Date: The Date data type is used to store a date value. Date values are represented in the format YYYY-MM-DD. For example, Date myDate = Date.newInstance(2023, 3, 23);

To summarize, the collection data types in Apex and Visualforce controllers include List, Set, and Map. String, Number, and Date are not collection data types.