Apex - 集合



集合是一種可以儲存多個記錄的變數型別。例如,列表可以儲存多個帳戶物件的記錄。現在讓我們詳細瞭解所有集合型別。

列表

列表可以包含任意數量的原始資料、集合、sObject、使用者定義和內建 Apex 型別的記錄。這是最重要的集合型別之一,並且它還有一些專門為列表使用而定製的系統方法。列表索引始終從 0 開始。這與 Java 中的陣列相同。列表應使用關鍵字“List”宣告。

示例

下面是包含原始資料型別(字串)列表的列表,即城市列表。

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

宣告列表的初始值是可選的。但是,我們將在此處宣告初始值。以下示例顯示了相同的內容。

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

帳戶列表(sObject)

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

我們也可以宣告巢狀列表。它最多可以巢狀五層。這稱為多維列表。

這是整數集合的列表。

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

列表可以包含任意數量的記錄,但為了防止效能問題和佔用資源,堆大小有限制。

列表方法

列表有一些可用方法,我們在程式設計時可以使用它們來實現某些功能,例如計算列表的大小、新增元素等。

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

  • 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);

您也可以使用陣列表示法來宣告列表,如下所示,但這在 Apex 程式設計中不是一般的做法:

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

集合

集合是一種包含多個無序唯一記錄的集合型別。集合不能包含重複記錄。與列表一樣,集合也可以巢狀。

示例

我們將定義公司銷售的產品集合。

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

集合方法

集合支援我們在程式設計時可以利用的方法,如下所示(我們擴充套件了上面的示例):

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

對映

它是一個鍵值對,每個值都包含唯一的鍵。鍵和值都可以是任何資料型別。

示例

以下示例表示產品名稱與產品程式碼的對映。

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

對映方法

以下是一些演示可以使用對映的方法的示例:

// 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。宣告為字串的對映鍵區分大小寫;例如,ABC 和 abc 將被視為不同的鍵並被視為唯一。

廣告