目录

Apex - Collections

集合是一种可以存储多个记录的变量。 例如,List可以存储多个Account对象的记录。 现在让我们详细了解所有集合类型。

Lists

List可以包含任何数量的基元,集合,sObject,用户定义和内置Apex类型的记录。 这是最重要的收集类型之一,并且它具有一些专门用于List的系统方法。 列表索引始终以0开头。这与Java中的数组同义。 应使用关键字“List”声明列表。

Example

下面是包含原始数据类型列表(字符串)的列表,即城市列表。

List<string> ListOfCities = new List<string>();
System.debug('Value Of ListOfCities'+ListOfCities);

声明list的初始值是可选的。 但是,我们将在此声明初始值。 以下是显示相同的示例。

List<string> ListOfStates = new List<string> {'NY', 'LA', 'LV'};
System.debug('Value ListOfStates'+ListOfStates);

List of Accounts (sObject)

List<account> AccountToDelete = new List<account> (); //This will be null
System.debug('Value AccountToDelete'+AccountToDelete);

我们也可以声明嵌套的List。 它可以达到五个级别。 这称为多维列表。

这是整数集的列表。

List<List<Set<Integer>>> myNestedList = new List<List<Set<Integer>>>();
System.debug('value myNestedList'+myNestedList);

List可以包含任意数量的记录,但是堆大小存在限制,以防止性能问题和独占资源。

列表方法

有可用于列表的方法,我们可以在编程时使用这些方法来实现一些功能,例如计算List的大小,添加元素等。

以下是一些最常用的方法 -

  • size()
  • add()
  • get()
  • clear()
  • set()

以下示例演示了所有这些方法的用法

// Initialize the List
List<string> ListOfStatesMethod = new List<string>();
// This statement would give null as output in Debug logs
System.debug('Value of List'+ ListOfStatesMethod);
// Add element to the list using add method
ListOfStatesMethod.add('New York');
ListOfStatesMethod.add('Ohio');
// This statement would give New York and Ohio as output in Debug logs
System.debug('Value of List with new States'+ ListOfStatesMethod);
// Get the element at the index 0
String StateAtFirstPosition = ListOfStatesMethod.get(0);
// This statement would give New York as output in Debug log
System.debug('Value of List at First Position'+ StateAtFirstPosition);
// set the element at 1 position
ListOfStatesMethod.set(0, 'LA');
// This statement would give output in Debug log
System.debug('Value of List with element set at First Position' + ListOfStatesMethod[0]);
// Remove all the elements in List
ListOfStatesMethod.clear();
// This statement would give output in Debug log
System.debug('Value of List'+ ListOfStatesMethod);

您也可以使用数组表示法来声明List,如下所示,但这不是Apex编程中的一般做法 -

String [] ListOfStates = new List<string>();

Sets

Set是一种集合类型,包含多个无序唯一记录。 集不能有重复记录。 与列表一样,集合可以嵌套。

Example

我们将定义公司销售的产品组合。

Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
System.debug('Value of ProductSet'+ProductSet);

集的方法

Set支持我们可以在编程时使用的方法,如下所示(我们正在扩展上面的例子) -

// Adds an element to the set
// Define set if not defined previously
Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
ProductSet.add('HCL');
System.debug('Set with New Value '+ProductSet);
// Removes an element from set
ProductSet.remove('HCL');
System.debug('Set with removed value '+ProductSet);
// Check whether set contains the particular element or not and returns true or false
ProductSet.contains('HCL');
System.debug('Value of Set with all values '+ProductSet);

Maps

它是一个键值对,包含每个值的唯一键。 键和值都可以是任何数据类型。

Example

以下示例使用产品代码表示产品名称的映射。

// Initialize the Map
Map<string, string> ProductCodeToProductName = new Map<string, string>
{'1000'=>'HCL', '1001'=>'H2SO4'};
// This statement would give as output as key value pair in Debug log
System.debug('value of ProductCodeToProductName'+ProductCodeToProductName);

地图方法

以下是一些示例,演示了可以与Map一起使用的方法 -

// Define a new map
Map<string, string> ProductCodeToProductName = new Map<string, string>();
// Insert a new key-value pair in the map where '1002' is key and 'Acetone' is value
ProductCodeToProductName.put('1002', 'Acetone');
// Insert a new key-value pair in the map where '1003' is key and 'Ketone' is value
ProductCodeToProductName.put('1003', 'Ketone');
// Assert that the map contains a specified key and respective value
System.assert(ProductCodeToProductName.containsKey('1002'));
System.debug('If output is true then Map contains the key and output is:'
   + ProductCodeToProductName.containsKey('1002'));
// Retrieves a value, given a particular key
String value = ProductCodeToProductName.get('1002');
System.debug('Value at the Specified key using get function: '+value);
// Return a set that contains all of the keys in the map
Set SetOfKeys = ProductCodeToProductName.keySet();
System.debug('Value of Set with Keys '+SetOfKeys);

映射值可能是无序的,因此我们不应该依赖于存储值的顺序,并且总是尝试使用键来访问映射。 映射值可以为null。 声明String时的映射键区分大小写; 例如,ABC和abc将被视为不同的键并被视为唯一键。

<上一篇.Apex - 循环
Apex - Classes.下一篇>
↑回到顶部↑
WIKI教程 @2018