Simple explanation about collections and its types in Apex:
Collections in Apex:
A collection is a type of variable that can store multiple number of records or items. Collections are work like arrays and they have more advanced behaviors and easier access methods than arrays. Data collections are groupings of any data types for example List can collect multiple number of sObjects records.
Types of collection:
List is an ordered group of items of the same data type that are distinguished by their indices. List elements can be of any data type like primitive types, collections, sObjects, user-defined types and built-in Apex types . Example for list is collection of Account records in Account object . Also List can contain duplicate values for example same Account repeated multiple times and lists are the mostly used collection in salesforce.
Syntax for List in String data type==>
List<String> myList=new List<String>();
Set is collection of unique, unordered elements of the same data type and set elements can be of any data type of primitive types, collections, sObjects, user-defined types and built-in Apex types. A set maintains no particular order for its elements. Because the elements are unordered, Also a set can’t have any duplicate values.
Syntax for Set String data type==>
Set<String> myList=new Set<String>();
Map is a collection of key-value pairs where each unique key maps to a single value. Also map is more complex collection than either List or Set. Map has the two major parts and they are key and value , called Key-Value pair. Keys and values can be any data type of primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Syntax for Map String data type==>
Map<String(key), String(value)> myMap= new Map<String, String>();