课程表

Apex课程

工具箱
速查手册

Apex - 集合

当前位置:免费教程 » 程序设计 » Apex

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


Lists 列表

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


例:

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

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


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

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


帐户列表(sObject):

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


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

这是整数集合的列表。

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

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


Methods For Lists 列表方法

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


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

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


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

  1. //Initialize the List
  2. List<string> ListOfStatesMethod = new List<string>();
  3.  
  4. //This statement would give null as output in Debug logs
  5. System.debug('Value of List'+ ListOfStatesMethod);
  6.  
  7. //Add element to the list using add method
  8. ListOfStatesMethod.add('New York');
  9. ListOfStatesMethod.add('Ohio');
  10.  
  11. //This statement would give New York and Ohio as output in Debug logs
  12. System.debug('Value of List with new States'+ ListOfStatesMethod);
  13.  
  14. //Get the element at the index 0
  15. String StateAtFirstPosition = ListOfStatesMethod.get(0);
  16.  
  17. //This statement would give New York as output in Debug log
  18. System.debug('Value of List at First Position'+ StateAtFirstPosition);
  19.  
  20. //set the element at 1 position
  21. ListOfStatesMethod.set(0, 'LA');
  22.  
  23. //This statement would give output in Debug log
  24. System.debug('Value of List with element set at First Position'+ ListOfStatesMethod[0]);
  25.  
  26. //Remove all the elements in List
  27. ListOfStatesMethod.clear();
  28.  
  29. //This statement would give output in Debug log
  30. System.debug('Value of List'+ ListOfStatesMethod);


你也可以使用数组符号来声明List,如下所示,但这不是Apex编程中的一般实践:

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


Sets 集合

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


例如:

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

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


Methods for Sets 集合的方法

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

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


Maps 地图

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


例如:

下面的示例表示产品名称与产品代码的映射。
  1. //Initialize the Map
  2. Map<string, string> ProductCodeToProductName = new Map<string, string> {'1000'=>'HCL', '1001'=>'H2SO4'};
  3.  
  4. //This statement would give as output as key value pair in Debug log
  5. System.debug('value of ProductCodeToProductName'+ProductCodeToProductName);


Methods for Maps 地图的方法

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

  1. // Define a new map
  2. Map<string, string> ProductCodeToProductName = new Map<string, string>(); 
  3.  
  4. // Insert a new key-value pair in the map where '1002' is key and 'Acetone' is value
  5. ProductCodeToProductName.put('1002', 'Acetone');
  6.  
  7. // Insert a new key-value pair in the map where '1003' is key and 'Ketone' is value 
  8. ProductCodeToProductName.put('1003', 'Ketone'); 
  9.  
  10. // Assert that the map contains a specified key and respective value
  11. System.assert(ProductCodeToProductName.containsKey('1002')); 
  12. System.debug('If output is true then Map contains the key and output is :'+ProductCodeToProductName.containsKey('1002'));
  13.  
  14. // Retrieves a value, given a particular key
  15. String value = ProductCodeToProductName.get('1002'); 
  16. System.debug('Value at the Specified key using get function: '+value);
  17.  
  18. // Return a set that contains all of the keys in the map
  19. Set SetOfKeys = ProductCodeToProductName.keySet(); 
  20. System.debug('Value of Set with Keys '+SetOfKeys);

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

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号