当前位置:K88软件开发文章中心编程语言非主流编程语言Apex → 文章内容

Apex - 集合

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:30:45

由 kaikai0220 创建,youj 最后一次修改 2016-12-12 集合是可以存储多个记录数的变量类型。 例如,List可以存储多个Account对象的记录。 让我们详细了解所有集合类型。 Lists 列表列表可以包含任何数量的原始,集合,sObjects,用户定义和内置的Apex类型的记录。 这是一个最重要的类型的集合,并且,它有一些系统方法,已经专门定制使用List。 列表索引总是从0开始。这与Java中的数组是同义的。 列表应使用关键字“List”声明。 例: 下面是包含一个原始数据类型列表(字符串)的列表,这是城市列表。 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);帐户列表(sObject): List<account> AccountToDelete = new List<account> ();//This will be nullSystem.debug(' Value AccountToDelete'+AccountToDelete);我们可以声明嵌套List。 它可以达到五级。 这被称为多维列表。 这是整数集合的列表。 List<List<Set<Integer>>> myNestedList = new List<List<Set<Integer>>>();System.debug('value myNestedList'+myNestedList);列表可以包含任何数量的记录,但是对堆大小有一个限制,以防止性能问题和独占资源。 Methods For Lists 列表方法有可用于列表的方法,我们可以在编程时使用以实现某些功能,例如计算List的大小,添加元素等。 下面是一些最常用的方法。size()add()get()clear()set()以下示例演示如何使用所有这些方法://Initialize the ListList<string> ListOfStatesMethod = new List<string>();//This statement would give null as output in Debug logsSystem.debug('Value of List'+ ListOfStatesMethod);//Add element to the list using add methodListOfStatesMethod.add('New York');ListOfStatesMethod.add('Ohio');//This statement would give New York and Ohio as output in Debug logsSystem.debug('Value of List with new States'+ ListOfStatesMethod);//Get the element at the index 0String StateAtFirstPosition = ListOfStatesMethod.get(0);//This statement would give New York as output in Debug logSystem.debug('Value of List at First Position'+ StateAtFirstPosition);//set the element at 1 positionListOfStatesMethod.set(0, 'LA');//This statement would give output in Debug logSystem.debug('Value of List with element set at First Position'+ ListOfStatesMethod[0]);//Remove all the elements in ListListOfStatesMethod.clear();//This statement would give output in Debug logSystem.debug('Value of List'+ ListOfStatesMethod);你也可以使用数组符号来声明List,如下所示,但这不是Apex编程中的一般实践: String [] ListOfStates = new List<string>();Sets 集合Set是集合类型,它包含多个无序的唯一记录数。 集合不能具有重复记录。 像列表一样,集合可以嵌套。 例如: 我们将定义公司销售的一系列产品。Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};System.debug('Value of ProductSet'+ProductSet);Methods for Sets 集合的方法Set支持我们可以在编程时使用的方法,如下所示(我们将扩展上面的示例): //Adds an element to the set//Define set if not defined previouslySet<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};ProductSet.add('HCL');System.debug('Set with New Value '+ProductSet);//Removes an element from setProductSet.remove('HCL');System.debug('Set with removed value  '+ProductSet);//Check whether set contains the particular element or not and returns true or falseProductSet.contains('HCL');System.debug('Value of Set with all values '+ProductSet);Maps 地图它是一个键值对,其中包含每个值的唯一键。 键和值都可以是任何数据类型。 例如: 下面的示例表示产品名称与产品代码的映射。//Initialize the MapMap<string, string> ProductCodeToProductName = new Map<string, string> {'1000'=>'HCL', '1001'=>'H2SO4'};//This statement would give as output as key value pair in Debug logSystem.debug('value of ProductCodeToProductName'+ProductCodeToProductName);Methods for Maps 地图的方法下面是一些例子,演示了我们可以使用Map的方法: // Define a new mapMap<string, string> ProductCodeToProductName = new Map<string, string>(); // Insert a new key-value pair in the map where '1002' is key and 'Acetone' is valueProductCodeToProductName.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 valueSystem.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 keyString 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 mapSet SetOfKeys = ProductCodeToProductName.keySet(); System.debug('Value of Set with Keys '+SetOfKeys);映射值可以是无序的,因此我们不应该依赖于值的存储顺序,并尝试总是使用键来访问映射。 地图值可以为null。 声明时映射键String是区分大小写的,例如ABC和abc将被视为不同的键,并被视为唯一。

Apex - 集合