Salesforce 配列初期化 簡略書き方

http://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/

省略初期化方法
set -> list :
Set setStrings = new Set{‘hello’,’world’};
List listStrings = new List(setStrings);

// create a new set with 3 elements
Set s = new Set{‘Jon’, ‘Quinton’, ‘Reid’};
// adds an element IF not already present
s.add(‘Sandeep’);
// adds an element IF not already present
s.add(‘Sandeep’);
// return the number of elements
System.debug(‘=== number of elements: ‘ + s.size());
// removes the element if present
s.remove(‘Reid’);
// return the number of elements
System.debug(‘=== number of elements: ‘ + s.size());
// returns true if the set contains the specified element
System.debug(‘=== set contains element Jon: ‘ + s.contains(‘Jon’));
// output all elements in the set
for (String str : s)
// outputs an element
System.debug(‘=== element : ‘ + str);
// makes a duplicate of the set
Set s1 = s.clone();
// displays the contents of the set
System.debug(‘=== contents of s1: ‘ + s1);
// removes all elements
s1.clear();
// returns true if the set has zero elements
System.debug(‘=== is the s1 set empty? ‘ + s1.isEmpty());

List s = new List{‘Jon’, ‘Quinton’, ‘Reid’};
// adds an element
s.add(‘Sandeep’);
// adds an element
s.add(‘Sandeep’);
// return the number of elements
System.debug(‘=== number of elements: ‘ + s.size());
// displays the first element
System.debug(‘=== first element: ‘ + s.get(0));
// removes the first element
s.remove(0);
// return the number of elements
System.debug(‘=== number of elements: ‘ + s.size());
// makes a duplicate of the set
List s1 = s.clone();
// displays the contents of the set
System.debug(‘=== contents of s1: ‘ + s1);
// replace the last instance of ‘Sandeep’ with ‘Pat’
s1.set(3,’Pat’); // displays the contents of the set
System.debug(‘=== contents of s1: ‘ + s1);
// sorts the items in ascending (primitave only)
s1.sort();
// displays the contents of the set
System.debug(‘=== sorted contents of s1: ‘ + s1);
// removes all elements
s.clear();
// returns true if the set has zero elements
System.debug(‘=== is the list empty? ‘ + s.isEmpty());

Map m = new Map{5 => ‘Jon’, 6 => ‘Quinton’, 1 => ‘Reid’};
// displays all keys
System.debug(‘=== all keys in the map: ‘ + m.keySet());
// displays all values
System.debug(‘=== all values in the map (as a List): ‘ + m.values());
// does the key exist?
System.debug(‘=== does key 6 exist?: ‘ + m.containsKey(6));
// fetches the value for the key
System.debug(‘=== value for key 6: ‘ + m.get(6));
// adds a new key/value pair
m.put(3,’Dave’);
// returns the number of elements
System.debug(‘=== size after adding Dave: ‘ + m.size());
// removes an element
m.remove(5);
System.debug(‘=== size after removing Jon: ‘ + m.size());
// clones the map
Map m1 = m.clone();
System.debug(‘=== cloned m1: ‘ + m1);
// removes all elements
m.clear();
// returns true if zero elements
System.debug(‘=== is m empty? ‘ + m.isEmpty());

コメントを残す

メールアドレスが公開されることはありません。