Thursday, July 7, 2016

Salesforce Collection Types

In Salesforce we have 3 collection types

  • List
  • Map
  • Set
  • Ordered collection of typed primitives, sObjects, objects, or collections that are distinguished by their indices
// Create an empty list of String
List<String> myList = new List<String>();
myList.add('hi');
String x = myList.get(0);

// Create list of records from a query
List<Account> accs =[SELECT Id, Name FROM Account LIMIT 1000];
  • Collection of keyvalue pairs where each unique key maps to a single value. A key can be any primitive data type except Blob and Object, while a value can be a primitive, an sObject, a collection type, or an object.
Map<String,String> MyStrings =new Map<String,String>{'a' => 'b', 'c' =>'d'.toUpperCase()};
Account myAcct = new Account();
Map<Integer, Account> m =new Map<Integer,Account>();
m.put(1, myAcct);
Set
  • Unordered collection that doesn’t contain any duplicate elements.
Set<Integer> s = new Set<Integer>();
s.add(12);
s.add(12);
System.assert(s.size()==1);

No comments:

Post a Comment