Apex-collections

提供:Dev Guides
移動先:案内検索

Apex-コレクション

コレクションは、複数のレコードを保存できる変数の一種です。 たとえば、Listは複数のAccountオブジェクトのレコードを保存できます。 次に、すべてのコレクションタイプの詳細な概要を説明します。

リスト

リストには、プリミティブ、コレクション、sObject、ユーザー定義およびApexタイプで作成されたレコードをいくつでも含めることができます。 これはコレクションの最も重要なタイプの1つであり、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 null
System.debug('Value AccountToDelete'+AccountToDelete);

ネストされたリストも宣言できます。 最大5つのレベルに移動できます。 これは多次元リストと呼ばれます。

これは整数のセットのリストです。

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

リストには任意の数のレコードを含めることができますが、パフォーマンスの問題とリソースの独占を防ぐために、ヒープサイズに制限があります。

リストのメソッド

リストのサイズの計算、要素の追加などの機能を実現するためにプログラミング中に利用できるリストのメソッドがあります。

以下は、最も頻繁に使用されるいくつかの方法です-

  • サイズ()
  • add()
  • 取得する()
  • clear()
  • セット()

次の例は、これらすべての方法の使用法を示しています

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

Sets

セットは、複数の番号なしの一意のレコードを含むコレクションタイプです。 セットに重複レコードを含めることはできません。 リストと同様に、セットはネストできます。

会社が販売している製品のセットを定義します。

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

Maps

これは、各値の一意のキーを含むキーと値のペアです。 キーと値の両方に任意のデータ型を使用できます。

次の例は、製品コードと製品名のマップを表しています。

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

マップ値は順序付けられていない可能性があるため、値が格納されている順序に依存してはならず、常にキーを使用してマップにアクセスしようとします。 マップ値はヌルにすることができます。 Stringを宣言するときのマップキーでは大文字と小文字が区別されます。たとえば、ABCとabcは異なるキーと見なされ、一意として扱われます。