×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex 集合


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


Lists 列表

列表可以包含任何数量的原始,集合,sObjects,用户定义和内置的Apex类型的记录。 这是一个最重要的类型的集合,并且,它有一些系统方法,已经专门定制使用List。 列表索引总是从0开始。这与Java中的数组是同义的。 列表应使用关键字“List”声明。


例:

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

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


声明list的初始值是可选的,但是我们可以这样做。 下面是同样的例子。

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


帐户列表(sObject):

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


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

这是整数集合的列表。

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

列表可以包含任何数量的记录,但是对堆大小有一个限制,以防止性能问题和独占资源。


Methods For Lists 列表方法

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


下面是一些最常用的方法。

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


以下示例演示如何使用所有这些方法:

//Initialize the List
List ListOfStatesMethod = new List();

//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();


Sets 集合

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


例如:

我们将定义公司销售的一系列产品。

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


Methods for Sets 集合的方法

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

//Adds an element to the set
//Define set if not defined previously
Set ProductSet = new Set{'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 地图

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


例如:

下面的示例表示产品名称与产品代码的映射。
//Initialize the Map
Map ProductCodeToProductName = new Map {'1000'=>'HCL', '1001'=>'H2SO4'};

//This statement would give as output as key value pair in Debug log
System.debug('value of ProductCodeToProductName'+ProductCodeToProductName);


Methods for Maps 地图的方法

下面是一些例子,演示了我们可以使用Map的方法:

// Define a new map
Map ProductCodeToProductName = new Map(); 

// 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将被视为不同的键,并被视为唯一。


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)